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
- [Sec_a]
- A_key1 = 20
- A_key2 = 10
- [Sec_b]
- B_key1 = 121
- B_key2 = B_value2
- B_key3 = $r
- B_key4 = 127.0.0.1
Test file test.py
[Python]View PlainCopy
- #-*-Coding:utf-8-*-
- Import Configparser
- #生成config对象
- conf = Configparser.configparser ()
- #用config对象读取配置文件
- Conf.read ("test.cfg")
- #以列表形式返回所有的section
- Sections = Conf.sections ()
- Print ' sections: ', sections #sections: [' sec_b ', ' sec_a ']
- #得到指定section的所有option
- Options = Conf.options ("sec_a")
- Print ' options: ', Options #options: [' a_key1 ', ' A_key2 ']
- #得到指定section的所有键值对
- KVS = Conf.items ("sec_a")
- Print ' sec_a: ', KVS #sec_a: [(' A_key1 ', ' ') ', (' A_key2 ', ' ten ')]
- #指定section, option to read the value
- Str_val = Conf.get ("sec_a", "A_key1")
- Int_val = Conf.getint ("sec_a", "A_key2")
- Print "value for Sec_a ' s a_key1:", Str_val #value for sec_a ' s a_key1:20
- Print "value for Sec_a ' s A_key2:", Int_val #value for sec_a ' s a_key2:10
- #写配置文件
- #更新指定section, the value of option
- Conf.set ("Sec_b", "B_key3", "new-$r")
- #写入指定section增加新option和值
- Conf.set ("Sec_b", "B_newkey", "New-value")
- #增加新的section
- Conf.add_section (' a_new_section ')
- Conf.set (' a_new_section ', ' New_key ', ' New_value ')
- #写回配置文件
- Conf.write (Open ("test.cfg", "W"))
2.2, Test 2
configuration file Test.cfg
[Plain]View PlainCopy
- [INFO]
- Age = 21
- Name = Chen
- Sex = Male
Test file test.py
[Python]View PlainCopy
- From __future__ import with_statement
- Import Configparser
- Config=configparser.configparser ()
- With open ("test.cfg","RW") as Cfgfile:
- CONFIG.READFP (Cfgfile)
- Name=config.get ("info","name")
- Age=config.get ("info","age")
- Print name
- Print Age
- Config.set ("info","sex", "male")
- Config.set ("info","age"," the")
- Age=config.getint ("info","age")
- Print name
- print type (age)
- 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
- [Portal]
- url = http://% (host) s:% (port) s/portal
- host = localhost
- Port = 8080
Using Rawconfigparser:
[Python]View PlainCopy
- Import Configparser
- conf = Configparser.rawconfigparser ()
- Print "Use Rawconfigparser () read"
- Conf.read ("test.cfg")
- Print Conf.get ("Portal", "url")
- Print "Use Rawconfigparser () write"
- Conf.set ("Portal", "Url2", "% (host) s:% (port) S")
- Print Conf.get ("Portal", "Url2")
Get output
[Plain]View PlainCopy
- Use Rawconfigparser () read
- http://% (host) s:% (port) s/portal
- Use Rawconfigparser () write
- % (host) s:% (port) s
Switch to Configparser
[Python]View PlainCopy
- Import Configparser
- conf = Configparser.configparser ()
- Print "Use Rawconfigparser () read"
- Conf.read ("test.cfg")
- Print Conf.get ("Portal", "url")
- Print "Use Rawconfigparser () write"
- Conf.set ("Portal", "Url2", "% (host) s:% (port) S")
- Print Conf.get ("Portal", "Url2")
Get output
[Plain]View PlainCopy
- Use Rawconfigparser () read
- Http://localhost:8080/Portal
- Use Rawconfigparser () write
- localhost:8080
Switch to safeconfigparser with the same effect as Configparser
[Python]View PlainCopy
- Import Configparser
- conf = Configparser.safeconfigparser ()
- Print "Use Rawconfigparser () read"
- Conf.read ("test.cfg")
- Print Conf.get ("Portal", "url")
- Print "Use Rawconfigparser () write"
- Conf.set ("Portal", "Url2", "% (host) s:% (port) S")
- Print Conf.get ("Portal", "Url2")
Conclusion:
Or with Configparser?
Python-configparser module "read-write configuration file"