in python, you may need to parse the config file sometime in your work. for example let the user config the app or server. you need to write config file like .ini or xml file.

this article will show you how to use simple .ini file and parse it using python.

there are library to help, before we start to use the library, we should know a little about the config file.

" config.ini

[section]
key=value
.
.
.
[section]
key=value

when using the library ConfigParser, there are some method may confuse you. something like: options, sections.

  • options: items under the section
  • section: this will appear in .ini file which surround with []

we start with this simple config file:

[EDITOR]
pdf=adobe
word=office
ppt=office
[UNKNOW]
name=someone

now I will show you what’s the section and option:

def parse_config(config_file):
    """parse the config

    """
    parser = ConfigParser.ConfigParser()
    parser.read(config_file)

    # show the section
    print parser.sections()

    # show the of the section
    for section in parser.sections():
        print parser.options(section)

you will see the output here:

['EDITOR', 'UNKNOW']
['pdf', 'word', 'ppt']
['name']

get and value of the option:

# get the value of the config
for section in parser.sections():
    print section
    for (key, value) in parser.items(section):
        print key, " : ", value

set the value of the option:

parser.set(section, key, value)

save the config file at last:

parser.write(open(config_file, 'w'))

to find more information about the usage, you can forward to the doc.

reference

https://docs.python.org/2/library/configparser.html