Skip to content Skip to sidebar Skip to footer

Parse A Custom Text File In Python

I have a text to be parsed, this is a concise form of the text. apple { type=fruit varieties { color=red origin=usa } } the output should be as shown b

Solution 1:

Since your file is in HOCON format, you can try using the pyhocon HOCON parser module to solve your problem.

Install: Either run pip install pyhocon, or download the github repo and perform a manual install with python setup.py install.

Basic usage:

from pyhocon import ConfigFactory

conf = ConfigFactory.parse_file('text.conf')

print(conf)

Which gives the following nested structure:

ConfigTree([('apple', ConfigTree([('type', 'fruit'), ('varieties', ConfigTree([('color', 'red'), ('origin', 'usa')]))]))])

ConfigTree is just a collections.OrderedDict(), as seen in the source code.

UPDATE:

To get your desired output, you can make your own recursive function to collect all paths:

from pyhocon import ConfigFactory
from pyhocon.config_tree import ConfigTree

def config_paths(config):
    for k, v in config.items():
        if isinstance(v, ConfigTree):
            for k1, v1 in config_paths(v):
                yield (k,) + k1, v1
        else:
            yield (k,), v

config = ConfigFactory.parse_file('text.conf')
for k, v in config_paths(config):
    print('%s=%s' % ('.'.join(k), v))

Which Outputs:

apple.type=fruit
apple.varieties.color=red
apple.varieties.origin=usa

Solution 2:

@RoadRunner How to parse the arrays inside the conf file ? for ex:

apple {
    type=fruit
    varieties {
        color=red
        origin=usa
    }
}
loo = [
{
name = "abc"
},
{
name = "xyz"
}
]

I want to print all the name values ie , abc and xyz


Post a Comment for "Parse A Custom Text File In Python"