Configparser Write configuration file Chaos sequence problem
In a Centos6.5 environment, the configuration file is typically parsed using Configparser. The Python version of Centos6.5 is Python 2.6.6.
The order of configuration files in general scenarios is not so important, but the order of configuration files in some scenarios is very effective, especially if the value of the configuration item has override capabilities.
Take the following example to illustrate:
- [b]
- Y1 = 10
- x2 = 20
- Z1 = 30
- A
- x2 = 40
- Z2 = 10
- Y1 = 10
Common configuration file parsing methods in CentOS 6.5 are 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 shown below
- Import Configparser
- Config = Configparser.configparser ()
- FP = open (R "/root/test/ceph.conf", "R")
- CONFIG.READFP (FP)
- Sections = Config.sections ()
- Print Sections
The above output shows that the section order of the configuration file is B, a, and the actual output is a, B. For a general scenario, it doesn't matter, but in the included scenario, such as B is a generic configuration, and A is a special configuration, A's configuration can overwrite the contents of some of the configuration items in B, and there is a problem. The root cause of this problem is that in Configparser, the default is to use the dict to save the parsed data, and dict itself is unordered, in fact, according to the order of the key values are saved, so there is a A, a, a, B. This can also lead to a disorderly ordering of the configuration files.
In fact, according to the official documentation, you can set the Dict_type parameter of the Configparser and change the corresponding dictionary type to solve this sequence problem. Changedinversion2.6:dict_typewasadded.
Changedinversion2.7:thedefaultdict_typeiscollections. ordereddict.allow_no_valuewasadded. Tested in Python version 2.7, the configuration file does not appear to be out of order, so you can pass the 2.7 parameter 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:
- 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
http://www.bkjia.com/PHPjc/1108024.html www.bkjia.com true http://www.bkjia.com/PHPjc/1108024.html techarticle configparser write config file in the Centos6.5 environment, usually use Configparser to parse the configuration file. The Python version of Centos6.5 is Python 2.6.6. For the general should ...