smallfish bio photo

smallfish

Engineer at Alibaba, Taobao.com #golang #clojure #nginx-lua #postgres

Email Twitter Github

默认的ConfigParser对于选项是按照字母顺序排列的。如下代码:

>>> from ConfigParser import ConfigParser
>>> cf = ConfigParser()
>>> cf.add_section('d')
>>> cf.set('d', 'name', 'smallfish')
>>> cf.add_section('a')
>>> cf.set('a', 'name', 'smallfish2')
>>> cf.write(open('d:/a.ini', 'w'))
>>> cf = None

生成配置如下:

[a]
name = smallfish2
[d]
name = smallfish

翻阅了官方文档似乎对ConfigParser中section的顺序没啥解说,毕竟字典本身就是无序的,如果想修改估计只能从源码入手把。不过有一个ConfigObj库还不错,可以实现顺序,当然功能不仅仅如此啦。下载地址:http://www.voidspace.org.uk/python/configobj.html

代码片段如下:

>>> from configobj import ConfigObj
>>> config = ConfigObj()
>>> config.filename = 'd:/a.ini'
>>> config['d'] = {}
>>> config['d']['name'] = 'smallfish'
>>> config['a'] = {}
>>> config['a']['name'] = 'smallfish2'
>>> config.write()

生成配置如下:

[d]
name = smallfish
[a]
name = smallfish2