在Python ConfigParser中按字母顺序导出 [英] Export with alphabetical sort in Python ConfigParser
本文介绍了在Python ConfigParser中按字母顺序导出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有任何解决方案可以强制RawConfigParser.write()方法以字母顺序导出配置文件?
Is there any solution to force the RawConfigParser.write() method to export the config file with an alphabetical sort?
即使原始/加载的配置文件是排序后,该模块将部分和选项随意混合到部分中,真的很烦人,手动编辑一个巨大的未排序配置文件。
Even if the original/loaded config file is sorted, the module mixes the section and the options into the sections arbitrarily, and is really annoying to edit manually a huge unsorted config file.
PD:我使用的是python 2.6
PD: I'm using python 2.6
推荐答案
三种解决方案:
- 传入dict类型(构造函数的第二个参数),该类型将按您喜欢的排序顺序返回键。
- 扩展类并重载
write()
(只需从原始来源复制此方法并进行修改)。 - 复制文件ConfigParser.py并将排序添加到方法
write ()
。
- Pass in a dict type (second argument to the constructor) which returns the keys in your preferred sort order.
- Extend the class and overload
write()
(just copy this method from the original source and modify it). - Copy the file ConfigParser.py and add the sorting to the method
write()
.
请参见本文以获得有序的字典,或者使用此实现可保留原始添加顺序。
See this article for a ordered dict or maybe use this implementation which preserves the original adding order.
这篇关于在Python ConfigParser中按字母顺序导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文