Python-configparser module "read-write configuration file"

Source: Internet
Author: User

1, Function Introduction

1.1. Read the 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 and returns the INT type

1.2. Writing the configuration file

-add_section (section) Add a new section
-set (section, option, value) to set the option in sections
You need to call write to write the content to the configuration file.

2, test Example 2.1, test 1

Configuration file Test.cfg

[Plain]View PlainCopy
    1. [Sec_a]
    2. A_key1 = 20
    3. A_key2 = 10
    4. [Sec_b]
    5. B_key1 = 121
    6. B_key2 = B_value2
    7. B_key3 = $r
    8. B_key4 = 127.0.0.1

Test file test.py

[Python]View PlainCopy
  1. #-*-Coding:utf-8-*-
  2. Import Configparser
  3. #生成config对象
  4. conf = Configparser.configparser ()
  5. #用config对象读取配置文件
  6. Conf.read ("test.cfg")
  7. #以列表形式返回所有的section
  8. Sections = Conf.sections ()
  9. Print ' sections: ', sections #sections: [' sec_b ', ' sec_a ']
  10. #得到指定section的所有option
  11. Options = Conf.options ("sec_a")
  12. Print ' options: ', Options #options: [' a_key1 ', ' A_key2 ']
  13. #得到指定section的所有键值对
  14. KVS = Conf.items ("sec_a")
  15. Print ' sec_a: ', KVS #sec_a: [(' A_key1 ', ' ') ', (' A_key2 ', ' ten ')]
  16. #指定section, option to read the value
  17. Str_val = Conf.get ("sec_a", "A_key1")
  18. Int_val = Conf.getint ("sec_a", "A_key2")
  19. Print "value for Sec_a ' s a_key1:", Str_val #value for sec_a ' s a_key1:20
  20. Print "value for Sec_a ' s A_key2:", Int_val #value for sec_a ' s a_key2:10
  21. #写配置文件
  22. #更新指定section, the value of option
  23. Conf.set ("Sec_b", "B_key3", "new-$r")
  24. #写入指定section增加新option和值
  25. Conf.set ("Sec_b", "B_newkey", "New-value")
  26. #增加新的section
  27. Conf.add_section (' a_new_section ')
  28. Conf.set (' a_new_section ', ' New_key ', ' New_value ')
  29. #写回配置文件
  30. Conf.write (Open ("test.cfg", "W"))

2.2, Test 2 configuration file Test.cfg

[Plain]View PlainCopy
    1. [INFO]
    2. Age = 21
    3. Name = Chen
    4. Sex = Male

Test file test.py

[Python]View PlainCopy
  1. From __future__ import with_statement
  2. Import Configparser
  3. Config=configparser.configparser ()
  4. With open ("test.cfg","RW") as Cfgfile:
  5. CONFIG.READFP (Cfgfile)
  6. Name=config.get ("info","name")
  7. Age=config.get ("info","age")
  8. Print name
  9. Print Age
  10. Config.set ("info","sex", "male")
  11. Config.set ("info","age"," the")
  12. Age=config.getint ("info","age")
  13. Print name
  14. print type (age)
  15. Print Age

Analysis

the info in [] is the name of this configuration.

where age,name are attributes.

First, Config=configparser.configparser () Gets a configuration config object. The following opens a configuration file, Cfgfile. Use READFP () to read this file. The contents of this configuration are enrolled in the Config object.

The next question is how to read the value. Common methods are get () and Getint (). Get () returns text. Getint () returns an integer.

Second, name=config.get ("info", "name") means to read the name variable value in the Info section of CONFIG.

Finally, how to set the value. Set the variable using set (segment name, variable name, value). Config.set ("info", "Age", "21") indicates that the age variable in the info section is set to 21.

2.3, Test 3

3 classes are defined in Python's Configparser module 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.

Configuration file Test.cfg

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

Using Rawconfigparser:

[Python]View PlainCopy
  1. Import Configparser
  2. conf = Configparser.rawconfigparser ()
  3. Print "Use Rawconfigparser () read"
  4. Conf.read ("test.cfg")
  5. Print Conf.get ("Portal", "url")
  6. Print "Use Rawconfigparser () write"
  7. Conf.set ("Portal", "Url2", "% (host) s:% (port) S")
  8. Print Conf.get ("Portal", "Url2")

Get output

[Plain]View PlainCopy
    1. Use Rawconfigparser () read
    2. http://% (host) s:% (port) s/portal
    3. Use Rawconfigparser () write
    4. % (host) s:% (port) s


Switch to Configparser

[Python]View PlainCopy
  1. Import Configparser
  2. conf = Configparser.configparser ()
  3. Print "Use Rawconfigparser () read"
  4. Conf.read ("test.cfg")
  5. Print Conf.get ("Portal", "url")
  6. Print "Use Rawconfigparser () write"
  7. Conf.set ("Portal", "Url2", "% (host) s:% (port) S")
  8. Print Conf.get ("Portal", "Url2")

Get output

[Plain]View PlainCopy
    1. Use Rawconfigparser () read
    2. Http://localhost:8080/Portal
    3. Use Rawconfigparser () write
    4. localhost:8080


Switch to safeconfigparser with the same effect as Configparser

[Python]View PlainCopy
  1. Import Configparser
  2. conf = Configparser.safeconfigparser ()
  3. Print "Use Rawconfigparser () read"
  4. Conf.read ("test.cfg")
  5. Print Conf.get ("Portal", "url")
  6. Print "Use Rawconfigparser () write"
  7. Conf.set ("Portal", "Url2", "% (host) s:% (port) S")
  8. Print Conf.get ("Portal", "Url2")

Conclusion:

Or with Configparser?

Python-configparser module "read-write configuration file"

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.