ConfigParser写配置文件乱序问题_PHP教程
ConfigParser写配置文件乱序问题
在Centos6.5的环境下,通常使用ConfigParser进行配置文件的解析。Centos6.5的Python版本为Python 2.6.6。对于一般的应用场景中配置文件的顺序没有那么的重要,但有些场景中配置文件的顺序是非常有效的,特别是当配置项的值具有覆盖功能时这种问题更加的严重。
以下面的例子为例进行说明:
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>[b]</li><li>y1 = 10</li><li>x2 = 20</li><li>z1 = 30</li><li></li><li>[a]</li><li>x2 = 40</li><li>z2 = 10</li><li>y1 = 10</li></ol>
[root@stcell03 test]# python
Python 2.6.6 (r266:84292, Nov 22 2013, 12:16:22)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ConfigParser
>>> config = ConfigParser.ConfigParser()
>>> fp = open(r"/root/test/test.conf", "r")
>>> config.readfp(fp)
>>> sections = config.sections()
>>> print sections
['a', 'b']
>>>
具体代码如下所示
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>import ConfigParser<br /></li><li>config = ConfigParser.ConfigParser()<br /></li><li>fp = open(r"/root/test/ceph.conf", "r")<br /></li><li>config.readfp(fp)<br /></li><li>sections = config.sections()<br /></li><li>print sections</li></ol>
实际上根据官方的文档可知,可以设置ConfigParser的dict_type参数,改变对应的字典类型,从而解决这种序列问题。Changedinversion2.6:dict_typewasadded.
Changedinversion2.7:Thedefaultdict_typeiscollections.OrderedDict.allow_no_valuewasadded.经过测试在Python 2.7的版本中,配置文件不会出现乱序问题,因此可以在Python 2.6的版本中传递2.7的参数。如下所示:
[root@stcell03 test]# python
Python 2.6.6 (r266:84292, Nov 22 2013, 12:16:22)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ConfigParser
>>> from collections import OrderedDict
>>> config = ConfigParser.ConfigParser(dict_type=OrderedDict)
>>> fp = open(r"/root/test/test.conf", "r")
>>> config.readfp(fp)
>>> sections = config.sections()
>>> print sections
['b', 'a']
>>>
具体代码如下:
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>import ConfigParser<br /></li><li>from collections import OrderedDict<br /></li><li>config = ConfigParser.ConfigParser(dict_type=OrderedDict)<br /></li><li>fp = open(r"/root/test/test.conf", "r")<br /></li><li>config.readfp(fp)<br /></li><li>sections = config.sections()<br /></li><li>print sections</li></ol>

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

最近几天,Ice Universe 不断披露有关 Galaxy S25 Ultra 的详细信息,人们普遍认为这款手机将是三星的下一款旗舰智能手机。除此之外,泄密者声称三星只计划升级一款相机

OnLeaks 现在与 Android Headlines 合作,首次展示了 Galaxy S25 Ultra,几天前,他试图从他的 X(以前的 Twitter)粉丝那里筹集到 4,000 美元以上的资金,但失败了。对于上下文,嵌入在 h 下面的渲染图像

除了发布两款新智能手机外,TCL 还发布了一款名为 NXTPAPER 14 的新 Android 平板电脑,其大屏幕尺寸是其卖点之一。 NXTPAPER 14 采用 TCL 标志性品牌哑光液晶面板 3.0 版本

最近几天,Ice Universe 不断披露有关 Galaxy S25 Ultra 的详细信息,人们普遍认为这款手机将是三星的下一款旗舰智能手机。除此之外,泄密者声称三星只计划升级一款相机

Vivo Y300 Pro刚刚全面亮相,它是最薄的中端Android手机之一,配备大电池。准确来说,这款智能手机的厚度仅为 7.69 毫米,但配备了 6,500 mAh 的电池。这与最近推出的容量相同

三星尚未就何时更新其 Fan Edition (FE) 智能手机系列提供任何提示。目前来看,Galaxy S23 FE 仍然是该公司的最新版本,于 2023 年 10 月年初推出。

Redmi Note 14 Pro Plus 现已正式成为去年 Redmi Note 13 Pro Plus 的直接后继产品(亚马逊售价 375 美元)。正如预期的那样,Redmi Note 14 Pro Plus与Redmi Note 14和Redmi Note 14 Pro一起成为Redmi Note 14系列的主角。李

摩托罗拉今年发布了无数设备,尽管其中只有两款是可折叠的。就上下文而言,虽然世界上大多数地区都收到了 Razr 50 和 Razr 50 Ultra,但摩托罗拉在北美提供了 Razr 2024 和 Razr 2
