范例:
[sectionA]
var1=toto
var2=titi
homer=simpson
[sectionB]
var3=kiki
var4=roro
john=doe
通过下面的代码读取后,返回结果如下:
In section sectionB
Key john has value doe
Key var3 has value kiki
Key var4 has value roro
In section sectionA
Key homer has value simpson
Key var1 has value toto
Key var2 has value titi
也可以只读取其中一个字段:
>>> print config.get(“sectionB”,”john”)
doe
#!/usr/bin/python # -*- coding: iso-8859-1 -*- import ConfigParser # Open a configuration file config = ConfigParser.SafeConfigParser() config.read("config.ini") # Read the whole configuration file for section in config.sections(): print "In section %s" % section for (key, value) in config.items(section): print " Key %s has value %s" % (key, value)