python解析模块(ConfigParser)使用方法

虽然春天的劲风,着实让我不太喜欢,但不得不说,春天是个好季节,俗话说"一年之际在于春",春天是一年的开始,也是让人充满希望和幻想的季节。刚刚历经了寒冬的人们,开始在春天慢慢苏醒,他们慢慢的重拾自己的梦想,渐渐骨起斗志,一步步朝着自己的梦想出发,一点点靠近理想的终端。

测试配置文件test.conf内容如下:


[first]
w = 2
v: 3
c =11-3

[second]

sw=4
test: hello

测试配置文件中有两个区域,first和second,另外故意添加一些空格、换行。

下面解析:


>>> import ConfigParser
>>> conf=ConfigParser.ConfigParser()
>>> conf.read('test.conf')
['test.conf']
>>> conf.sections() #获得所有区域
['first', 'second']
>>> for sn in conf.sections():
... print conf.options(sn) #打印出每个区域的所有属性
...
['w', 'v', 'c']
['sw', 'test']

获得每个区域的属性值:


for sn in conf.sections():
print sn,'-->'
for attr in conf.options(sn):
print attr,'=',conf.get(sn,attr)

输出:


first -->
w = 2
v = 3
c = 11-3
second -->
sw = 4
test = hello

好了,以上就是基本的使用过程,下面是动态的写入配置,


cfd=open('test2.ini','w')
conf=ConfigParser.ConfigParser()
conf.add_section('test') #add a section
conf.set('test','run','false')
conf.set('test','set',1)
conf.write(cfd)
cfd.close()

上面是向test2.ini写入配置信息。

标签: python ConfigParser