The Configparser module in Python uses the detailed

Source: Internet
Author: User
Tags ini

This article mainly introduces the use of Configparser modules in Python, configparser modules are mainly used to read and write configuration modules, the need for friends can refer to the

1. Basic Read configuration file

-read (filename) Read the contents of the INI file directly

-sections () Gets all of the section and returns in the form of a list

-options (section) gets all option for this section

-items (section) gets all the key values of the section

-get (section,option) Gets the value of option in section, returned as String type

-getint (section,option) Gets the value of option in the section, returns to the int type, and the corresponding Getboolean () and GetFloat () functions.

2. Basic Write Configuration file

-add_section (section) Add a new section

-set (section, option, value) sets the option in sections to call write to write content to the configuration file.

3. Basic examples

?

1 2 3 4 5 6 7 8 9 10 test.conf [sec_a] a_key1 = A_key2 = ten [sec_b] b_key1 = 121 B_key2 = b_value2 B_key3 = $r B_key4 = 127.0.0.1

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The

Get terminal output:

?

1 2 3 4 5 Sections: [' Sec_b ', ' sec_a '] options: [' a_key1 ', ' A_key2 '] sec_a: [(' A_key1 ', ' I ' m value '), (' A_key2 ', ']] value for SE C_a ' s a_key1:i ' m value value for sec_a ' s a_key2:22

Test.conf after the update

?

1 2 3 4 5 6 7 8 9 10 11 12-13 [Sec_b] B_newkey = new-value B_key4 = 127.0.0.1 B_key1 = 121 B_key2 = b_value2 B_key3 = new-$r [sec_a] a_key1 = i ' M valu e A_key2 = [A_new_section] New_key = New_value

In the 4.Python Configparser module, 3 classes are defined to operate on the INI file. respectively, Rawconfigparser, Configparser, Safeconfigparser. Rawcnfigparser is the most basic INI file reading class, Configparser, Safeconfigparser supports parsing of% (value) s variables.

Set configuration file test2.conf

?

1 2 3 4 [Portal] url = http://% (host) s:% (port) s/portal host = localhost port = 8080

Use Rawconfigparser:

?

1 2 3 4 5 6 7 8 9 10 11 Import Configparser CF = Configparser.rawconfigparser () print "Use Rawconfigparser () read" Cf.read ("test2.conf") print Cf.get ("Portal", "url") print "use Rawconfigparser () write" Cf.set ("Portal", "url2", "% (host) s:% (port) S") Print Cf.get ( "Portal", "Url2")

Get terminal output:

?

1 2 3 4 Use Rawconfigparser () read http://% (host) s:% (port) s/portal use Rawconfigparser () write% (host) s:% (port) s

Switch to Configparser:

?

1 2 3 4 5 6 7 8 9 10 11 Import Configparser CF = Configparser.configparser () print "Use Configparser () read" Cf.read ("test2.conf") print cf.ge T ("Portal", "url") print "use Configparser () write" Cf.set ("Portal", "url2", "% (host) s:% (port) S") Print Cf.get ("Portal", "Url2")

Get terminal output:

?

1 2 3 4 Use Configparser () Read http://localhost:8080/Portal use Configparser () write localhost:8080

Switch to Safeconfigparser:

?

1 2 3 4 5 6 7 8 9 10 11 Import Configparser CF = Configparser.safeconfigparser () print "Use Safeconfigparser () read" Cf.read ("test2.conf") pri NT Cf.get ("Portal", "url") print "use Sateconfigparser () write" Cf.set ("Portal", "url2", "% (host) s:% (port) S") Print CF.G ET ("Portal", "Url2")

Get the terminal output (the effect is the same configparser):

?

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.