Python 之ConfigParser(读取配置文件)


一、ConfigParser简介

ConfigParser 是用来读取配置文件的包。配置文件的格式如下:中括号“[ ]”内包含的为section。section 下面为类似于key-value 的配置内容。

1
2
3
4
5
6
7
8
9
[db]
db_host  =  127.0 . 0.1
db_port  =  22
db_user  =  root
db_pass  =  rootroot
 
[concurrent]
thread  =  10
processor  =  20

中括号“[ ]”内包含的为section。紧接着section 为类似于key-value 的options 的配置内容。

二、ConfigParser 初始工作

使用ConfigParser 首选需要初始化实例,并读取配置文件:

1
2
cf  =  ConfigParser.ConfigParser()
cf.read( "配置文件名" )

三、ConfigParser 常用方法

1. 获取所有sections。也就是将配置文件中所有“[ ]”读取到列表中:

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

将输出(以下将均以简介中配置文件为例):

1
section: [ 'db' 'concurrent' ]

2. 获取指定section 的options。即将配置文件某个section 内key 读取到列表中:

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

将输出:

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

3. 获取指定section 的配置信息

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

将输出:

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

4. 按照类型读取指定section 的option 信息

同样的还有getfloat、getboolean。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#可以按照类型读取出来
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" )
 
# 返回的是整型的
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

将输出:

1
2
3
4
5
6
db_host:  127.0 . 0.1
db_port:  22
db_user: root
db_pass: rootroot
thread:  10
processor:  20

注:ConfigParser option 无论大小写,都会转换成小写,看了源代码发现了一行这样的代码

1
2
def  optionxform( self , optionstr): 
         return  optionstr.lower()

解决办法:

1.修改源代码,好处,方便快捷,但是不好处,别人使用会受影响

2.继承重写方法的方式,代码如下所示:

1
2
3
4
5
6
7
8
9
import  ConfigParser
 
class  myconf(ConfigParser.ConfigParser): 
     def  __init__( self ,defaults = None ): 
         ConfigParser.ConfigParser.__init__( self ,defaults = None
     def  optionxform( self , optionstr): 
         return  optionstr
   
conf = ConfigParser.ConfigParser()

 

5. 设置某个option 的值。(记得最后要写回)

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

6.添加一个section。(同样要写回)

1
2
3
4
5
6
7
8
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. 移除section 或者option 。(只要进行了修改就要写回的哦)

1
2
3
cf.remove_option( 'liuqing' , 'int' )
cf.remove_section( 'liuqing' )
cf.write( open ( "test.conf" "w" ))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/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" ))

  

原文参考于:http://blog.csdn.net/windone0109/article/details/10550383 

猜你喜欢

转载自blog.csdn.net/songruibb/article/details/80843976