Python reads and writes the configuration file-ConfigParser,
Python reads and writes data to the configuration file-ConfigParser
Python is easy to read and write configuration files. You can use the built-in configparser module in lib. For example, the local address of the blogger: "C:/python27/lib/configparser. py"
Configuration file parser.
A setup file consists of sections, lead by a "[section]" header,
and followed by "name: value" entries, with continuations and such in
the style of RFC 822.
This module supports reading configuration files similar to the preceding format, such as. conf and. ini files in windows.
This section uses the following configuration file as an example. You can create a Pyhton_config folder under drive D and create two files: test. config and test. ini. The content and example are as follows:
1 [db] 2 db_port = 33063 db_user = root4 db_host = 127.0.0.15 db_pass = xgmtest6 7 [concurrent] 8 processor = 209 thread = 10View Code
This section describes how to read and write configuration files.
Basic read configuration file
- -Read (filename) Directly read the file content.
- -Sections ()Get all sections and return them as a list
- -Options (section)Obtain all options for this section.
- -Items (section)Obtain all key-value pairs of this section.
- -Get (section, option)Obtain the option value in section. The return value is of the string type.
- -Getint (section, option)Get the option value in section, and return the int type, as well as the corresponding getboolean () and getfloat () functions.
1 #! /Usr/bin/env python 2 #-*-coding: UTF-8-*-3 4 import ConfigParser 5 import OS 6 7 OS. chdir ("D :\\ Python_config") 8 9 cf = ConfigParser. configParser () 10 11 # cf. read ("test. ini ") 12 cf. read ("test. conf ") 13 14 # return all section15 secs = cf. sections () 16 print 'sets', secs, type (secs) 17 opts = cf. options ("db") 18 print 'Options: ', opts, type (opts) 19 kvs = cf. items ("db") 20 print 'db: ', kvs21 22 # read by type23 db_host = cf. get ("db", "db_host") 24 db_port = cf. getint ("db", "db_port") 25 db_user = cf. get ("db", "db_user") 26 db_pass = cf. get ("db", "db_pass") 27 28 # read int29 threads = cf. getint ("concurrent", "thread") 30 processors = cf. getint ("concurrent", "processor") 31 print "db_host:", db_host32 print "db_port:", db_port33 print "db_user:", db_user34 print "db_pass :", db_pass35 print "thread:", threads36 print "processor:", processorsConfigParser
The sample code can be found at the top and parsed as follows:
It needs to be instantiated as the ConfigParser object cf = ConfigParser. ConfigParser (); read the file cf. read ("test. conf ")
Secs = cf. sections () Get sections and return list
Opts = cf. options ("db"): Obtain the options in the db section and return the list
Kvs = cf. items ("db") obtains all key-value pairs in the db section and returns the list as follows. Each list element is a key-Value Pair tuples.
Generally, we know the section and option and need to retrieve the corresponding value. The reading method is as follows:
#read by typedb_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")
Cf. get (...) returns the str type, while getint returns the int type.
Basic write configuration file
- -Write (fp)Write the config object to a. init file.Write an. ini-format representation of the configuration state.
- -Add_section (section)Add a new section
- -Set (section, option, valueTo set the option in the section, you must call write to write the content to the configuration file 1 import ConfigParser 2 import OS 3 4 OS. chdir ("D :\\ Python_config") 5 6 cf = ConfigParser. configParser () 7 8 cf. add_section ("test") 9 cf. set ("test", "count", 1) 10 cf. add_section ("test1") 11 cf. set ("test1", "name", "aaa") 12 13 # modify one value and write to file14 with open ("test2.ini", "w +") as f: 15 cf. write (f)ConfigParser2
- -Remove_section (section)Delete A section
- -Remove_option (section, option)Delete option under a section
The sample code is as follows:
1 import ConfigParser 2 import OS 3 4 OS. chdir ("D :\\ Python_config") 5 6 cf = ConfigParser. configParser () 7 8 # add section/set option & key 9 cf. add_section ("test") 10 cf. set ("test", "count", 1) 11 cf. add_section ("test1") 12 cf. set ("test1", "name", "aaa") 13 14 # write to file15 with open ("test2.ini", "w +") as f: 16 cf. write (f)ConfigParser2
The written files include:
Modification is similar to writing. Be sure to read the original file!
1 import ConfigParser 2 import OS 3 4 OS. chdir ("D :\\ Python_config") 5 6 cf = ConfigParser. ConfigParser () 7 8 # modify cf, be sure to read! 9 cf. read ("test2.ini") 10 cf. set ("test", "count", 2) # set to modify11 cf. remove_option ("test1", "name") 12 13 # write to file14 with open ("test2.ini", "w +") as f: 15 cf. write (f)ConfigParser3
After the above code is executed: the value of count under [test] is 2; The name key value under [test1] is deleted. to delete the entire [test1] section, use cf. remove_section ("test1 ")