The Python configuration file parsing module ConfigParser USES instances

  • 2020-05-09 18:45:21
  • OfStack

1. ConfigParser profile

ConfigParser is a package for reading configuration files. The configuration file is formatted as follows: section is contained in the brackets "[]". The following is the configuration content for section similar to key-value.


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

The brackets "[]" contain section. This is followed by the configuration of section for options similar to key-value.
 
2. ConfigParser initial work

The preferred way to use ConfigParser is to initialize the instance and read the configuration file:


 cf = ConfigParser.ConfigParser()
 cf.read(" Profile name ")

3. Common ConfigParser methods

1. Obtain all sections. Read all the "[]" in the configuration file into the list:


 s = cf.sections()
 print 'section:', s

Will output (as shown in the profile below) :

 section: ['db', 'concurrent']

2. Gets options for section. To read key in an section configuration file into the list:

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

The output:

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

3. Get the configuration information for the specified section.

 v = cf.items("db")
 print 'db:', v

The output:

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

4. Read option information specified by type for section.
The same goes for getfloat, getboolean.

 # It can be read by type
 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")
 
 # It's an integer
 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

The output:

 db_host: 127.0.0.1
 db_port: 22
 db_user: root
 db_pass: rootroot
 thread: 10
 processor: 20

5. Set a value of option. (remember to write back at the end)

 cf.set("db", "db_pass", "zhaowei")
 cf.write(open("test.conf", "w"))

6. Add one section. (also write back)

 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. (write it back as soon as you modify it)

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


#!/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"))


Related articles: