ConfigParser writes configuration files out of order problem
In the Centos6.5 environment, ConfigParser is usually used to parse configuration files. The Python version of Centos6.5 is Python 2.6.6.
The order of configuration files is not that important in general application scenarios, but in some scenarios the order of configuration files is very effective, especially when the value of the configuration item has an override function. This problem is even more serious. serious.
Take the following example as an example to illustrate:
<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>
Copy after login
The commonly used configuration file parsing method in Centos 6.5 is as follows:
[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']
>>>
The specific code is as follows
<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>
Copy after login
As can be seen from the above output, the configuration file The section order is b, a, and the actual output section is a, b. It doesn't matter in general scenarios, but in included scenarios, for example, b is a general configuration, and a is a special configuration, and the configuration of a can override the contents of some configuration items in b, problems will arise at this time. The root cause of this problem is that ConfigParser uses dict to save the parsed data by default, and dict itself is unordered. It is actually saved according to the order of key values, so the order of a and b appears. This may also cause configuration files to be out of order.
In fact, according to the official documentation, you can set the dict_type parameter of ConfigParser and change the corresponding dictionary type to solve this sequence problem. Changedinversion2.6:dict_typewasadded.
Changedinversion2.7:Thedefaultdict_typeiscollections.OrderedDict.allow_no_valuewasadded. After testing, in the Python 2.7 version, the configuration file will not be out of order, so the 2.7 parameters can be passed in the Python 2.6 version. As shown below:
[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']
>>>
The specific code is as follows:
<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>
Copy after login
http://www.bkjia.com/PHPjc/1108024.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1108024.htmlTechArticleConfigParser writes configuration files out of order. In the Centos6.5 environment, ConfigParser is usually used to parse configuration files. The Python version of Centos6.5 is Python 2.6.6. For general applications...