Introduction of Configparser
Configparser is the package that is used to read the configuration file. The configuration file is formatted as follows: A section is included in the brackets "[]". The section below is similar to key-value configuration content.
Copy Code code as follows:
[db]
Db_host = 127.0.0.1
Db_port = 22
Db_user = root
Db_pass = Rootroot
[Concurrent]
Thread = 10
Processor = 20
The section enclosed in the brackets "[]" is included. The section is followed by the configuration of the options similar to the Key-value.
Second, Configparser initial work
Initializes the instance using the Configparser preference and reads the configuration file:
Copy Code code as follows:
CF = Configparser.configparser ()
Cf.read ("Profile name")
Iii. Common methods of Configparser
1. Get all sections. That is, all of the "[]" in the configuration file is read to the list:
Copy Code code as follows:
s = cf.sections ()
print ' section: ', S
will be output (as an example of the profiles in the profile below):
Copy Code code as follows:
Section: [' db ', ' concurrent ']
2. Gets the options for the specified section. To read a section key in the configuration file to the list:
Copy Code code as follows:
o = cf.options ("db")
print ' Options: ', O
Will output:
Copy Code code as follows:
Options: [' db_host ', ' db_port ', ' db_user ', ' Db_pass ']
3. Gets the configuration information for the specified section.
Copy Code code as follows:
v = cf.items ("db")
print ' db: ', V
Will output:
Copy Code code as follows:
DB: [(' Db_host ', ' 127.0.0.1 '), (' Db_port ', ') ', (' Db_user ', ' Root '), (' Db_pass ', ' rootroot ')]
4. Reads the option information for the specified section by type.
The same goes for GetFloat, Getboolean.
Copy Code code as follows:
#可以按照类型读取出来
Db_host = Cf.get ("db", "Db_host")
Db_port = Cf.getint ("db", "Db_port")
Db_user = Cf.get ("db", "Db_user")
Db_pass = Cf.get ("db", "Db_pass")
# returns an integral type.
Threads = Cf.getint ("Concurrent", "thread")
processors = Cf.getint ("Concurrent", "processor")
Print "Db_host:", Db_host
Print "Db_port:", Db_port
Print "Db_user:", Db_user
Print "Db_pass:", Db_pass
Print "Thread:", Threads
Print "Processor:", processors
Will output:
Copy Code code as follows:
db_host:127.0.0.1
Db_port:22
Db_user:root
Db_pass:rootroot
Thread:10
Processor:20
5. Set the value of an option. (Remember the last thing to write back)
Copy Code code as follows:
Cf.set ("db", "Db_pass", "Zhaowei")
Cf.write (Open ("test.conf", "w"))
6. Add a section. (also to write back)
Copy Code code as follows:
Cf.add_section (' liuqing ')
Cf.set (' liuqing ', ' int ', ' 15 ')
Cf.set (' liuqing ', ' bool ', ' true ')
Cf.set (' liuqing ', ' float ', ' 3.1415 ')
Cf.set (' liuqing ', ' baz ', ' fun ')
Cf.set (' liuqing ', ' Bar ', ' Python ')
Cf.set (' liuqing ', ' foo ', '% (bar) s is% (baz) s! ')
Cf.write (Open ("test.conf", "w"))
7. Remove section or option. (As long as the changes will be written back to the OH)
Copy Code code as follows:
Cf.remove_option (' liuqing ', ' int ')
Cf.remove_section (' liuqing ')
Cf.write (Open ("test.conf", "w"))
Copy Code code as follows:
#!/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 ')
Copy Code code as follows:
#!/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"))