The test configuration file test.conf The following contents:
Copy Code code as follows:
[A]
W = 2
V:3
C =11-3
[Second]
Sw=4
Test:hello
The test configuration file has two areas, first and second, and intentionally adds some spaces and wraps.
The following resolution:
Copy Code code as follows:
>>> Import Configparser
>>> Conf=configparser.configparser ()
>>> conf.read (' test.conf ')
[' test.conf ']
>>> conf.sections () #获得所有区域
[' A ', ' second ']
>>> for SN in Conf.sections ():
... print conf.options (SN) #打印出每个区域的所有属性
...
[' W ', ' V ', ' C ']
[' SW ', ' Test ']
To get the property values for each zone:
Copy Code code as follows:
For SN in conf.sections ():
Print sn, '--> '
For attr in Conf.options (SN):
Print attr, ' = ', Conf.get (sn,attr)
Output:
Copy Code code as follows:
The-->
W = 2
v = 3
c = 11-3
Second-->
SW = 4
Test = Hello
Okay, here's the basic usage, and here's the dynamic write configuration,
Copy Code code as follows:
Cfd=open (' Test2.ini ', ' W ')
Conf=configparser.configparser ()
Conf.add_section (' Test ') #add section A
Conf.set (' Test ', ' run ', ' false ')
Conf.set (' Test ', ' Set ', 1)
Conf.write (CFD)
Cfd.close ()
The above is to write configuration information to Test2.ini.