python-ConfigParser模块【读写配置文件】

http://www.codesky.net/article/201003/122500.html html

http://www.linuxso.com/linuxbiancheng/8987.html python

如下的文章就是对python 读写配置文件的具体方案的介绍 linux

1,函数介绍

1.1.读取配置文件

-read(filename)  直接读取ini文件内容
-sections()  获得全部的section,并以列表的形式返回
-options(section)  获得该section的全部option
-items(section)  获得该section的全部键值对
-get(section,option)  获得section中option的值,返回为string类型
-getint(section,option)  获得section中option的值,返回为int类型
 

1.2.写入配置文件

-add_section(section)  添加一个新的section
-set( section, option, value) 对section中的option进行设置
  须要调用write将内容写入配置文件。

2,测试实例

2.1,测试1

配置文件test.cfg 函数

[plain]   view plain copy
  1. [sec_a]  
  2. a_key1 = 20  
  3. a_key2 = 10  
  4.   
  5. [sec_b]  
  6. b_key1 = 121  
  7. b_key2 = b_value2  
  8. b_key3 = $r  
  9. b_key4 = 127.0.0.1  
测试文件test.py
[python]   view plain copy
  1. # -* - coding: UTF-8 -* -  
  2. import ConfigParser  
  3. #生成config对象  
  4. conf = ConfigParser.ConfigParser()  
  5. #用config对象读取配置文件  
  6. conf.read("test.cfg")  
  7. #以列表形式返回全部的section  
  8. sections = conf.sections()  
  9. print 'sections:', sections         #sections: ['sec_b', 'sec_a']  
  10. #获得指定section的全部option  
  11. options = conf.options("sec_a")  
  12. print 'options:', options           #options: ['a_key1', 'a_key2']  
  13. #获得指定section的全部键值对  
  14. kvs = conf.items("sec_a")  
  15. print 'sec_a:', kvs                 #sec_a: [('a_key1', '20'), ('a_key2', '10')]  
  16. #指定section,option读取值  
  17. str_val = conf.get("sec_a""a_key1")  
  18. int_val = conf.getint("sec_a""a_key2")  
  19.   
  20. print "value for sec_a's a_key1:", str_val   #value for sec_a's a_key1: 20  
  21. print "value for sec_a's a_key2:", int_val   #value for sec_a's a_key2: 10  
  22.   
  23. #写配置文件  
  24. #更新指定section,option的值  
  25. conf.set("sec_b""b_key3""new-$r")  
  26. #写入指定section增长新option和值  
  27. conf.set("sec_b""b_newkey""new-value")  
  28. #增长新的section  
  29. conf.add_section('a_new_section')  
  30. conf.set('a_new_section''new_key''new_value')  
  31. #写回配置文件  
  32. conf.write(open("test.cfg""w"))  

2.2,测试2

配置文件test.cfg
[plain]   view plain copy
  1. [info]  
  2. age = 21  
  3. name = chen  
  4. sex = male  

测试文件test.py 测试

[python]   view plain copy
  1. from __future__ import with_statement  
  2. import ConfigParser  
  3. config=ConfigParser.ConfigParser()  
  4. with open("test.cfg","rw") as cfgfile:  
  5.     config.readfp(cfgfile)  
  6.     name=config.get("info","name")  
  7.     age=config.get("info","age")  
  8.     print name  
  9.     print age  
  10.     config.set("info","sex","male")  
  11.     config.set("info","age","55")  
  12.     age=config.getint("info","age")  
  13.     print name  
  14.     print type(age)  
  15.     print age  

分析 url

其中[ ] 中的info是这段配置的名字。 spa

其中age,name都是属性。 .net

首先,config=ConfigParser.ConfigParser() 获得一个配置config对象.下面打开一个配置文件 cfgfile. 用readfp()读取这个文件.这样配置的内容就读到config对象里面了。 code

接下来一个问题是如何读取值.经常使用的方法是get() 和getint() . get()返回文本. getint()返回整数。 htm

其次,name=config.get(''info'',''name'')  意思就是.读取config中info段中的name变量值。

最后讲讲如何设置值.使用set(段名,变量名,值) 来设置变量.config.set(''info'',''age'',''21'') 表示把info段中age变量设置为21。


2.3,测试3

Python的ConfigParser Module中定义了3个类对INI文件进行操做。

分别是RawConfigParser、ConfigParser、SafeConfigParser。

RawCnfigParser是最基础的INI文件读取类,ConfigParser、SafeConfigParser支持对%(value)s变量的解析。 

配置文件test.cfg
[plain]   view plain copy
  1. [portal]  
  2. url = http://%(host)s:%(port)s/Portal  
  3. host = localhost  
  4. port = 8080  
使用RawConfigParser:
[python]   view plain copy
  1. import ConfigParser  
  2.   
  3. conf = ConfigParser.RawConfigParser()  
  4. print "use RawConfigParser() read"  
  5. conf.read("test.cfg")  
  6. print conf.get("portal""url")  
  7.   
  8. print "use RawConfigParser() write"  
  9. conf.set("portal""url2""%(host)s:%(port)s")  
  10. print conf.get("portal""url2")  
获得输出
[plain]   view plain copy
  1. use RawConfigParser() read  
  2. http://%(host)s:%(port)s/Portal  
  3. use RawConfigParser() write  
  4. %(host)s:%(port)s  

改用ConfigParser
[python]   view plain copy
  1. import ConfigParser  
  2.   
  3. conf = ConfigParser.ConfigParser()  
  4. print "use RawConfigParser() read"  
  5. conf.read("test.cfg")  
  6. print conf.get("portal""url")  
  7.   
  8. print "use RawConfigParser() write"  
  9. conf.set("portal""url2""%(host)s:%(port)s")  
  10. print conf.get("portal""url2")  
获得输出
[plain]   view plain copy
  1. use RawConfigParser() read  
  2. http://localhost:8080/Portal  
  3. use RawConfigParser() write  
  4. localhost:8080  

改用SafeConfigParser,效果与ConfigParser相同
[python]   view plain copy
  1. import ConfigParser  
  2.   
  3. conf = ConfigParser.SafeConfigParser()  
  4. print "use RawConfigParser() read"  
  5. conf.read("test.cfg")  
  6. print conf.get("portal""url")  
  7.   
  8. print "use RawConfigParser() write"  
  9. conf.set("portal""url2""%(host)s:%(port)s")  
  10. print conf.get("portal""url2")  

结论:

仍是用ConfigParser