I. Introduction of Configparser
Configparser is the package that is used to read the configuration file. The format of the configuration file is as follows: the brackets "[]" are included in the section. section below is a configuration content similar to Key-value.
1: [db]
2:db_host = 127.0.0.1
3:db_port =
4:db_user = root
5:db_pass = Rootroot
6:
7: [Concurrent]
8:thread = Ten
9:processor =
The brackets "[]" are included in section. The section is followed by the configuration of options similar to Key-value.
Ii. Initial work of Configparser
Using Configparser preferred to initialize the instance and read the configuration file:
1:CF = Configparser.configparser ()
2:cf.read ("Profile name")
Iii. Common methods of Configparser
1. Get all sections. That is, all "[]" in the configuration file is read to the list:
1:s = cf.sections ()
2:print ' section: ', S
Output (The following will be the example of profiles in profile):
1:section: [' db ', ' concurrent ']
2. Gets the options for the specifiedsection. To read a key in a section of the configuration file into the list:
1:o = cf.options ("db")
2:print ' options: ', O
Will output:
1:options: [' db_host ', ' db_port ', ' db_user ', ' Db_pass ']
3. Gets the configuration information for the specified section.
1:v = Cf.items ("db")
2:print ' db: ', v
Will output:
1:db: [(' Db_host ', ' 127.0.0.1 '), (' Db_port ', ' ') ', (' Db_user ', ' Root '), (' Db_pass ', ' rootroot ')]
4. Read the option information for the specified section by type.
There are also getfloat and Getboolean.
1: #可以按照类型读取出来
2:db_host = Cf.get ("db", "Db_host")
3:db_port = Cf.getint ("db", "Db_port")
4:db_user = Cf.get ("db", "Db_user")
5:db_pass = Cf.get ("db", "Db_pass")
6:
7: # Returns the integer type
8:threads = cf.getint ("Concurrent", "thread")
9:processors = cf.getint ("Concurrent", "Processor")
Ten:
11:print "Db_host:", Db_host
12:print "Db_port:", Db_port
13:print "Db_user:", Db_user
14:print "Db_pass:", Db_pass
15:print "Thread:", Threads
16:print "Processor:", processors
Will output:
1:db_host:127.0.0.1
2:db_port:22
3:db_user:root
4:db_pass:rootroot
5:thread:10
6:processor:20
5. Set the value of an option . (Remember to write back at the end)
1:cf.set ("db", "Db_pass", "Zhaowei")
2:cf.write (Open ("test.conf", "W"))
6. Add asection. (also to write back)
1:cf.add_section (' liuqing ')
2:cf.set (' liuqing ', ' int ', ' a ')
3:cf.set (' liuqing ', ' bool ', ' true ')
4:cf.set (' liuqing ', ' float ', ' 3.1415 ')
5:cf.set (' liuqing ', ' baz ', ' fun ')
6:cf.set (' liuqing ', ' Bar ', ' Python ')
7:cf.set (' liuqing ', ' foo ', '% (bar) s is% (baz) s! ')
8:cf.write (Open ("test.conf", "W"))
7. Remove section or option. (as long as the changes will be written back to the OH)
1:cf.remove_option (' liuqing ', ' int ')
2:cf.remove_section (' liuqing ')
3:cf.write (Open ("test.conf", "w"))
Click (here) to collapse or open
- #!/usr/bin/env python
- From Configparser import Configparser
- Configfile= "F.txt"
- Config=configparser ()
- Config.read (ConfigFile)
- Print Config.get (' Messages ', ' greeting ')
- Radius=input (Config.get (' Messages ', ' questions ') + ')
- Print Config.get (' Messages ', ' result ')
- Print config.getfloat (' numbers ', ' pi ') *radius**2
- S=config.sections ()
- print ' section: ', S
- O=config.options (' Messages ')
- print ' Messages option: ', O
- V=config.items ("Messages")
- print ' Message de Xinxi: ', V
- Config.add_section (' liuyang1 ')
- Config.set (' liuyang1 ', ' int ', ' 15 ')
- Config.set (' Liuyang ' 1, ' hhhh ', ' Hello World ')
- Config.write (Open ("F.txt", "w"))
- Print Config.get (' liuyang1 ', ' int ')
- Print Config.get (' liuyang1 ', ' hhhh ')
- #!/usr/bin/env python
- Import Configparser
- Import Sys
- Config=configparser.configparser ()
- Config.add_section ("Book1")
- Config.set ("Book1", "title", "Hello World")
- Config.set ("Book1", "aut", "log")
- Config.write (Open ("F.txt", "w"))
Python's Configparser