Skip to content Skip to sidebar Skip to footer

Combine Template And `configparser`

I have a template file called foo.cfg: [Box] box.active={box_activate} resolution_tracker.active=true box.api_key={box_api_key} box.api_secret={box_api_secret} box.job_interval=480

Solution 1:

You can format the keys and use StringIO to create a file-like object to pass to ConfigParser's readfp method:

from StringIO import StringIO
# ...

with open('foo.cfg') as foo:
    fixed = foo.read().format(**keys)
parser = ConfigParser.ConfigParser()
parser.readfp(StringIO(fixed))

# ...

Post a Comment for "Combine Template And `configparser`"