Python中的ConfigParser模块使用详解


Posted in Python onMay 04, 2015

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类型,还有相应的getboolean()和getfloat() 函数。

2.基本的写入配置文件

    -add_section(section) 添加一个新的section

    -set( section, option, value) 对section中的option进行设置,需要调用write将内容写入配置文件。

 

3.基本例子

test.conf
[sec_a] 
a_key1 = 20 
a_key2 = 10 
  
[sec_b] 
b_key1 = 121 
b_key2 = b_value2 
b_key3 = $r 
b_key4 = 127.0.0.1
parse_test_conf.py
import ConfigParser 
cf = ConfigParser.ConfigParser() 
#read config 
cf.read("test.conf") 
# return all section 
secs = cf.sections() 
print 'sections:', secs 
  
opts = cf.options("sec_a") 
print 'options:', opts 
  
kvs = cf.items("sec_a") 
print 'sec_a:', kvs 
  
#read by type 
str_val = cf.get("sec_a", "a_key1") 
int_val = cf.getint("sec_a", "a_key2") 
  
print "value for sec_a's a_key1:", str_val 
print "value for sec_a's a_key2:", int_val 
  
#write config 
#update value 
cf.set("sec_b", "b_key3", "new-$r") 
#set a new value 
cf.set("sec_b", "b_newkey", "new-value") 
#create a new section 
cf.add_section('a_new_section') 
cf.set('a_new_section', 'new_key', 'new_value') 
  
#write back to configure file 
cf.write(open("test.conf", "w"))

得到终端输出:

sections: ['sec_b', 'sec_a'] 
options: ['a_key1', 'a_key2'] 
sec_a: [('a_key1', "i'm value"), ('a_key2', '22')] 
value for sec_a's a_key1: i'm value 
value for sec_a's a_key2: 22

更新后的test.conf

[sec_b] 
b_newkey = new-value 
b_key4 = 127.0.0.1 
b_key1 = 121 
b_key2 = b_value2 
b_key3 = new-$r 
  
[sec_a] 
a_key1 = i'm value 
a_key2 = 22 
  
[a_new_section] 
new_key = new_value

4.Python的ConfigParser Module中定义了3个类对INI文件进行操作。分别是RawConfigParser、ConfigParser、SafeConfigParser。RawCnfigParser是最基础的INI文件读取类,ConfigParser、SafeConfigParser支持对%(value)s变量的解析。

设定配置文件test2.conf

[portal] 
url = http://%(host)s:%(port)s/Portal 
host = localhost 
port = 8080

使用RawConfigParser:

import ConfigParser 
 
cf = ConfigParser.RawConfigParser() 
 
print "use RawConfigParser() read" 
cf.read("test2.conf") 
print cf.get("portal", "url") 
 
print "use RawConfigParser() write" 
cf.set("portal", "url2", "%(host)s:%(port)s") 
print cf.get("portal", "url2")

得到终端输出:

use RawConfigParser() read 
http://%(host)s:%(port)s/Portal 
use RawConfigParser() write 
%(host)s:%(port)s

改用ConfigParser:

import ConfigParser 
 
cf = ConfigParser.ConfigParser() 
 
print "use ConfigParser() read" 
cf.read("test2.conf") 
print cf.get("portal", "url") 
 
print "use ConfigParser() write" 
cf.set("portal", "url2", "%(host)s:%(port)s") 
print cf.get("portal", "url2")

得到终端输出:

use ConfigParser() read 
http://localhost:8080/Portal 
use ConfigParser() write 
localhost:8080

改用SafeConfigParser:

import ConfigParser 
 
cf = ConfigParser.SafeConfigParser() 
 
print "use SafeConfigParser() read" 
cf.read("test2.conf") 
print cf.get("portal", "url") 
 
print "use SateConfigParser() write" 
cf.set("portal", "url2", "%(host)s:%(port)s") 
print cf.get("portal", "url2")

得到终端输出(效果同ConfigParser):

use SafeConfigParser() read 
http://localhost:8080/Portal 
use SateConfigParser() write 
localhost:8080
Python 相关文章推荐
Python 基础之字符串string详解及实例
Apr 01 Python
Python编程之序列操作实例详解
Jul 22 Python
python中requests库session对象的妙用详解
Oct 30 Python
python 常见字符串与函数的用法详解
Nov 23 Python
matplotlib实现区域颜色填充
Mar 18 Python
PythonWeb项目Django部署在Ubuntu18.04腾讯云主机上
Apr 01 Python
基于python3 pyQt5 QtDesignner实现窗口化猜数字游戏功能
Jul 15 Python
解决Jupyter因卸载重装导致的问题修复
Apr 10 Python
Django配置Bootstrap, js实现过程详解
Oct 13 Python
Python中读取文件名中的数字的实例详解
Dec 25 Python
Python日志打印里logging.getLogger源码分析详解
Jan 17 Python
详解python的异常捕获
Mar 03 Python
Python的__builtin__模块中的一些要点知识
May 02 #Python
一些Python中的二维数组的操作方法
May 02 #Python
在Python的Tornado框架中实现简单的在线代理的教程
May 02 #Python
探究Python的Tornado框架对子域名和泛域名的支持
May 02 #Python
Python编程中运用闭包时所需要注意的一些地方
May 02 #Python
按日期打印Python的Tornado框架中的日志的方法
May 02 #Python
详细解读Python的web.py框架下的application.py模块
May 02 #Python
You might like
PHP 上传文件的方法(类)
2009/07/30 PHP
有关php运算符的知识大全
2011/11/03 PHP
浅析PHP的ASCII码转换类
2013/07/05 PHP
zf框架的zend_cache缓存使用方法(zend框架)
2014/03/14 PHP
php检测图片主要颜色的方法
2015/07/01 PHP
PHP那些琐碎的知识点(整理)
2017/05/20 PHP
用javascript连接access数据库的方法
2006/11/17 Javascript
jquery中使用ajax获取远程页面信息
2011/11/13 Javascript
js同比例缩放图片的小例子
2013/10/30 Javascript
JSON无限折叠菜单编写实例
2013/12/16 Javascript
纯js实现div内图片自适应大小(已测试,兼容火狐)
2014/06/16 Javascript
jQuery实现购物车数字加减效果
2015/03/14 Javascript
不想让浏览器运行javascript脚本的方法
2015/11/20 Javascript
JavaScript实现in-place思想的快速排序方法
2016/08/07 Javascript
BootStrap实现邮件列表的分页和模态框添加邮件的功能
2016/10/13 Javascript
Angularjs中的页面访问权限怎么设置
2016/11/11 Javascript
vue父组件向子组件传递多个数据的实例
2018/03/01 Javascript
解决在vue项目中webpack打包后字体不生效的问题
2018/09/01 Javascript
vue双向绑定及观察者模式详解
2019/03/19 Javascript
js计算最大公约数和最小公倍数代码实例
2019/09/11 Javascript
微信小程序自定义底部弹出框功能
2020/11/18 Javascript
基于Django模板中的数字自增(详解)
2017/09/05 Python
python用pandas数据加载、存储与文件格式的实例
2018/12/07 Python
python 读取修改pcap包的例子
2019/07/23 Python
关于PySnooper 永远不要使用print进行调试的问题
2021/03/04 Python
学生感冒英文请假条
2014/02/04 职场文书
意向书范文
2014/03/31 职场文书
老师对学生的寄语
2014/04/09 职场文书
管理标语大全
2014/06/24 职场文书
联片教研活动总结
2014/07/01 职场文书
民族学专业大学生职业规划范文:清晰未来的构想
2014/09/20 职场文书
党员教师四风问题对照检查材料
2014/09/26 职场文书
2015年度团总支工作总结
2015/04/23 职场文书
英语读书笔记
2015/07/02 职场文书
厉行节约工作总结
2015/08/12 职场文书
golang生成vcf通讯录格式文件详情
2022/03/25 Golang