Python implementation code for read-write configuration files with Configobj

Source: Internet
Author: User
Discover the lib,http://www.voidspace.org.uk/python/configobj.html of a simple and powerful read-write configuration file.
Personally, the biggest highlight is the self-brought format check feature, and support for complex nested formats, and it is quite easy to use.

Let's take a look at examples.
Read the file

The code is as follows:


From configobj import configobj
Config = configobj (filename)
#
value1 = config[' Keyword1 ']
value2 = config[' Keyword2 ']
#
Section1 = config[' Section1 ']
Value3 = section1[' Keyword3 ']
Value4 = section1[' Keyword4 ']
#
# you could also write
Value3 = config[' Section1 ' [' Keyword3 ']
Value4 = config[' Section1 ' [' Keyword4 ']

Write a file

The code is as follows:


From configobj import configobj
Config = Configobj ()
Config.filename = filename
#
config[' keyword1 '] = value1
config[' keyword2 '] = value2
#
config[' Section1 ' = {}
config[' Section1 ' [' keyword3 '] = Value3
config[' Section1 ' [' keyword4 '] = value4
#
Section2 = {
' Keyword5 ': Value5,
' Keyword6 ': Value6,
' sub-section ': {
' Keyword7 ': Value7
}
}
config[' section2 '] = Section2
#
config[' Section3 ' = {}
config[' section3 ' [' keyword 8 '] = [Value8, Value9, Value10]
config[' section3 ' [' keyword 9 '] = [Value11, Value12, Value13]
#
Config.write ()

For more information, please refer to the official Doc documentation.

  • 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.