Configparser explanation of the Python parsing configuration module

Source: Internet
Author: User
1. Basic Read configuration file

-read (filename) reads INI file contents directly

-sections () gets all the sections and returns as a list

-options (section) gets all the option of the section

-items (section) gets all the key-value pairs of the section

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

-getint (section,option) Gets the value of option in section, returns 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 the section and needs to call write to write the content to the configuration file.

3. Basic examples

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

parse_test_conf.py

Import Configparser CF = Configparser.configparser () #read config cf.read ("test.conf") # return all section secs = Cf.sect Ions () print ' sections: ', secs    opts = cf.options ("sec_a") print ' options: ', opts    KVS = Cf.items ("sec_a") print ' SE C_a: ', KVS    #read by Type Str_val = Cf.get ("Sec_a", "a_key1") Int_val = Cf.getint ("Sec_a", "A_key2")    print "Value f Or sec_a ' s a_key1: ", str_val print" value for sec_a ' s A_key2: ", int_val    #write config #update value cf.set (" Sec_b "," b _key3 "," new-$r ") #set a new value Cf.set (" Sec_b "," B_newkey "," New-value ") #create a new section cf.add_section (' A_new_se Ction ') cf.set (' a_new_section ', ' new_key ', ' new_value ')    #write back to configure File Cf.write (open ("test.conf", "W "))

Get terminal output:

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

Test.conf after the update

[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 va Lue A_key2 =    [a_new_section] New_key = New_value

The Configparser module in 4.Python defines 3 classes to operate on the INI file. Rawconfigparser, Configparser, Safeconfigparser, respectively. Rawcnfigparser is the most basic INI file read class, Configparser, Safeconfigparser supports parsing of the% (value) s variable.

Setting configuration file test2.conf

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

Using Rawconfigparser:

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:

Use Rawconfigparser () read http://% (host) s:% (port) s/portal use Rawconfigparser () write% (host) s:% (port) s

Use Configparser instead:

Import configparser   CF = Configparser.configparser ()   print "Use Configparser () read" Cf.read ("test2.conf") Print Cf.get ("Portal", "url")   print "Use Configparser () write" Cf.set ("Portal", "url2", "% (host) s:% (port) S") print Cf.get ("Portal", "Url2")

Get terminal output:

Use Configparser () Read http://localhost:8080/Portal use Configparser () write localhost:8080

Use Safeconfigparser instead:

Import configparser   CF = Configparser.safeconfigparser ()   print "Use Safeconfigparser () read" Cf.read (" Test2.conf ") Print Cf.get (" Portal "," url ")   print" Use Sateconfigparser () write "Cf.set (" Portal "," url2 ","% (host) s: % (port) S ") Print Cf.get (" Portal "," Url2 ")

Get terminal output (effect same as Configparser):

Use Safeconfigparser () Read http://localhost:8080/Portal use Sateconfigparser () write localhost:8080
  • 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.