SoFunction
Updated on 2024-11-20

Python configuration file parsing module ConfigParser example of use

I. Introduction to ConfigParser

ConfigParser is a package for reading configuration files. The format of a configuration file is as follows: sections are enclosed in brackets "[ ]", and below a section are key-value-like configuration contents.

Copy Code The code is as follows.

 [db]
 db_host = 127.0.0.1
 db_port = 22
 db_user = root
 db_pass = rootroot
 
 [concurrent]
 thread = 10
 processor = 20

Sections are enclosed in brackets "[ ]", followed by sections that are configured as key-value-like options.
 
II. ConfigParser Initial Work

To use ConfigParser, you need to initialize the instance and read the configuration file:

Copy Code The code is as follows.

 cf = ()
("Configuration File Name")

ConfigParser Common Methods

1. Get all the sections. i.e. read all the "[ ]" in the config file into the list:

Copy Code The code is as follows.

 s = ()
 print 'section:', s

will be output (the following will all be examples of profiles in the profile):
Copy Code The code is as follows.

 section: ['db', 'concurrent']

2. Get the options for the specified section, i.e., read the key from a section of the configuration file into the list:
Copy Code The code is as follows.

 o = ("db")
 print 'options:', o

Will output:
Copy Code The code is as follows.

 options: ['db_host', 'db_port', 'db_user', 'db_pass']

3. Get configuration information for the specified section.
Copy Code The code is as follows.

 v = ("db")
 print 'db:', v

Will output:
Copy Code The code is as follows.

 db: [('db_host', '127.0.0.1'), ('db_port', '22'), ('db_user', 'root'), ('db_pass', 'rootroot')]

4. Read the option information of the specified section by type.
The same goes for getfloat, getboolean.
Copy Code The code is as follows.

# Can be read out by type
 db_host = ("db", "db_host")
 db_port = ("db", "db_port")
 db_user = ("db", "db_user")
 db_pass = ("db", "db_pass")
 
# Returns an integer
 threads = ("concurrent", "thread")
 processors = ("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 The code is 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 to write back at the end)
Copy Code The code is as follows.

 ("db", "db_pass", "zhaowei")
 (open("", "w"))

6. Add a section. (Again, write back)
Copy Code The code is as follows.

 cf.add_section('liuqing')
 ('liuqing', 'int', '15')
 ('liuqing', 'bool', 'true')
 ('liuqing', 'float', '3.1415')
 ('liuqing', 'baz', 'fun')
 ('liuqing', 'bar', 'Python')
 ('liuqing', 'foo', '%(bar)s is %(baz)s!')
 (open("", "w"))

7. Remove the section or option. (You have to write back any changes you make.)
Copy Code The code is as follows.

 cf.remove_option('liuqing','int')
 cf.remove_section('liuqing')
 (open("", "w"))

Copy Code The code is as follows.

#!/usr/bin/env python
from ConfigParser import ConfigParser
CONFIGFILE=""
config=ConfigParser()
(CONFIGFILE)
print ('messages','greeting')
radius=input(('messages','questions')+' ')
print ('messages','result')
print ('numbers','pi')*radius**2

s=()
print'section: ',s
o=('messages')
print'messages option: ',o
v=("messages")
print'message de xinxi: ',v

config.add_section('liuyang1')
('liuyang1','int','15')
('liuyang'1,'hhhh','hello world')
(open("","w"))
print ('liuyang1','int')
print ('liuyang1','hhhh')


Copy Code The code is as follows.

#!/usr/bin/env python
import ConfigParser
import sys
config=()
config.add_section("book1")
("book1","title","hello world")
("book1","aut","log")
(open("","w"))