Configparser Writing configuration file _php Tutorial

Source: Internet
Author: User

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:
 
  
  
  1. [b]
  2. Y1 = 10
  3. x2 = 20
  4. Z1 = 30
  5. A
  6. x2 = 40
  7. Z2 = 10
  8. 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
 
  
  
  1. Import Configparser
  2. Config = Configparser.configparser ()
  3. FP = open (R "/root/test/ceph.conf", "R")
  4. CONFIG.READFP (FP)
  5. Sections = Config.sections ()
  6. 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:
 
  
  
  1. Import Configparser
  2. From collections Import Ordereddict
  3. Config = Configparser.configparser (dict_type=ordereddict)
  4. FP = open (R "/root/test/test.conf", "R")
  5. CONFIG.READFP (FP)
  6. Sections = Config.sections ()
  7. 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 ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.