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.