search
Homephp教程php手册ConfigParser写配置文件乱序问题

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>
在Centos 6.5常用的配置文件解析方法如下:
[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>
通过上述的输出可知,配置文件的section顺序为b, a,而实际输出的section为a, b。对于一般场景下无所谓,但在包含的场景中,比如b是一个通用的配置,而a是一个特殊的配置,a的配置能够覆盖b中某些配置项的内容,此时就会出现问题。出现这种问题的根本原因是在ConfigParser中默认采用了dict保存解析到的数据,而dict本身是无序的,实际上是根据键值的顺序保存,因此出现了a,b的顺序。这样也就可能导致配置文件的乱序。

实际上根据官方的文档可知,可以设置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>


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),