Differences between revisions 6 and 7
Revision 6 as of 2007-11-16 18:46:29
Size: 2925
Editor: host
Comment: changed formatting...
Revision 7 as of 2008-01-19 04:18:55
Size: 2925
Editor: cpe-74-74-227-99
Comment: typo fixed in ConfigSectionMap
Deletions are marked like this. Additions are marked like this.
Line 47: Line 47:
            Print("exception on %s!" % option)             print("exception on %s!" % option)

These are some examples on using ConfigParser, assuming the following INI file...

[SectionOne]
Status: Single
Name: Derek
Value: Yes
Age: 29
Single: True

[SectionTwo]
FavoriteColor=Green
[SectionThree]
FamilyName: Johnson

[Others]
Route: 66

>>> import ConfigParser
>>> Config = ConfigParser.ConfigParser()
>>> Config
<ConfigParser.ConfigParser instance at 0x00BA9B20>
>>> Config.read("c:\\tomorrow.ini")
['c:\\tomorrow.ini']
>>> Config.sections()
['Others', 'SectionThree', 'SectionOne', 'SectionTwo']
>>>

Explanation: We first import the configparser, tell it to read the file, and get a listing of the sections. Sections are listed in square brackets [].

Next, we are going to get some settings, after defining a helper function.

The Function:

def ConfigSectionMap(section):
    dict1 = {}
    options = Config.options(section)
    for option in options:
        try:
            dict1[option] = Config.get(section, option)
            if dict1[option] == -1:
                DebugPrint("skip: %s" % option)
        except:
            print("exception on %s!" % option)
            dict1[option] = None
    return dict1

Now the code:

>>>Name = ConfigSectionMap("SectionOne")['name']
>>>Age = ConfigSectionMap("SectionOne")['age']
>>>print "Hello %s. You are %s years old." % (Name, Age)
Hello Derek. You are 29 years old.


This works great most of the time, but what about the "Value: Yes" and "Single: True" values? Those are booleans. They can be either True or False, Yes or No, 1 or 0, on or off. To read a boolean value, you use:  Config.getboolean(section, option)  Example, continuing from above:

>>>single = Config.getboolean("SectionOne", "single")
>>>single
True

You can also use getint(section, option) to get a number as an int. This may be easier to use than int(Config.get(section, option)) There is also getfloat which is used the same as getint, but, as you guessed, returns a float instead of an int.


Notes on reading an INI file

lines beginning with a semicolon ';' a pound sign '#' or the letters 'REM' (uppercase or lowercase) will be ignored. You may use these for comments if you want. You cannot put a comment on an option line. It will only be treated as a comment if it is at the beginning of the line!


Writing an INI file

When you write to an INI file, you will wipe out all comments.


Assuming the config file doesn't exist yet, this is the code to create one:

# lets create that config file for next time...
cfgfile = open("c:\\next.ini",'w')

# add the settings to the structure of the file, and lets write it out...
Config.add_section('Person')
Config.set('Person','HasEyes',True)
Config.set('Person','Age', 50)
Config.write(cfgfile)
cfgfile.close()


CategoryDocumentation

ConfigParserExamples (last edited 2016-04-11 04:17:05 by bignose)

Unable to edit the page? See the FrontPage for instructions.