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 拷贝对象(深拷贝deepcopy与浅拷贝copy)
Sep 06 Python
跟老齐学Python之让人欢喜让人忧的迭代
Oct 02 Python
Python中使用Inotify监控文件实例
Feb 14 Python
进一步探究Python的装饰器的运用
May 05 Python
Python安装第三方库及常见问题处理方法汇总
Sep 13 Python
python中实现控制小数点位数的方法
Jan 24 Python
Flask框架重定向,错误显示,Responses响应及Sessions会话操作示例
Aug 01 Python
python实现大量图片重命名
Mar 23 Python
Python zip函数打包元素实例解析
Dec 11 Python
PyTorch学习:动态图和静态图的例子
Jan 06 Python
python 偷懒技巧——使用 keyboard 录制键盘事件
Sep 21 Python
Python判断变量是否是None写法代码实例
Oct 09 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生成4位数字验证码的实现代码
2015/11/23 PHP
浅谈php调用python文件
2019/03/29 PHP
使一个函数作为另外一个函数的参数来运行的javascript代码
2007/08/13 Javascript
JavaScript 组件之旅(二)编码实现和算法
2009/10/28 Javascript
JQuery 1.6发布 性能提升,同时包含大量破坏性变更
2011/05/10 Javascript
基于jquery的web页面日期格式化插件
2011/11/15 Javascript
使用js实现雪花飘落效果
2013/08/26 Javascript
原生javascript图片自动或手动切换示例附演示源码
2013/09/04 Javascript
JavaScript Ajax Json实现上下级下拉框联动效果实例代码
2013/11/23 Javascript
js中AppendChild与insertBefore的用法详细解析
2013/12/16 Javascript
js计算德州扑克牌面值的方法
2015/03/04 Javascript
原生JS实现图片左右轮播
2016/12/30 Javascript
JavaScript中 this 指向问题深度解析
2017/02/21 Javascript
详解vue-cli下ESlint 配置说明
2018/09/03 Javascript
微信{"errcode":48001,"errmsg":"api unauthorized, hints: [ req_id: 1QoCla0699ns81 ]"}
2018/10/12 Javascript
vue组件从开发到发布的实现步骤
2018/11/11 Javascript
mock.js模拟前后台交互
2019/07/25 Javascript
[02:07]2017国际邀请赛中国区预选赛直邀战队前瞻
2017/06/23 DOTA
基于pandas数据样本行列选取的方法
2018/04/20 Python
Python requests发送post请求的一些疑点
2018/05/20 Python
使用python脚本实现查询火车票工具
2018/07/19 Python
python+selenium实现QQ邮箱自动发送功能
2019/01/23 Python
详解python中的线程与线程池
2019/05/10 Python
解决django后台样式丢失,css资源加载失败的问题
2019/06/11 Python
python使用pymongo与MongoDB基本交互操作示例
2020/04/09 Python
如何理解python面向对象编程
2020/06/01 Python
基于Python词云分析政府工作报告关键词
2020/06/02 Python
python 爬虫请求模块requests详解
2020/12/04 Python
一款CSS3实现多功能下拉菜单(带分享按)的教程
2014/11/05 HTML / CSS
Europcar德国:全球汽车租赁领域的领导者
2018/08/15 全球购物
澳大利亚音乐商店:Bava’s Music City
2019/05/05 全球购物
手工制作的音乐盒:Music Box Attic
2019/09/05 全球购物
下面这个程序执行后会有什么错误或者效果
2014/11/03 面试题
简单的辞职信模板
2015/05/12 职场文书
人力资源部工作计划
2019/05/14 职场文书
什么是clearfix (一文搞清楚css清除浮动clearfix)
2023/05/21 HTML / CSS