Python parsing module (configparser) use method _python

Source: Internet
Author: User

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.

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.