是否有任何解决方案可以强制 RawConfigParser.write() 方法按字母顺序导出配置文件?
即使原始/加载的配置文件已排序,模块也会任意地将节和选项混合到节中,并且手动编辑巨大的未排序配置文件确实很烦人。
PD:我正在使用 python 2.6
我能够通过从外部对 ConfigParser 中的部分进行排序来解决这个问题,如下所示:
config = ConfigParser.ConfigParser({}, collections.OrderedDict)
config.read('testfile.ini')
# Order the content of each section alphabetically
for section in config._sections:
config._sections[section] = collections.OrderedDict(sorted(config._sections[section].items(), key=lambda t: t[0]))
# Order all sections alphabetically
config._sections = collections.OrderedDict(sorted(config._sections.items(), key=lambda t: t[0] ))
# Write ini file to standard output
config.write(sys.stdout)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)