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实现txt文件格式转换为arff格式
May 31 Python
python直接获取API传递回来的参数方法
Dec 17 Python
python+opencv 读取文件夹下的所有图像并批量保存ROI的方法
Jan 10 Python
python整小时 整天时间戳获取算法示例
Feb 20 Python
Python3.5以上版本lxml导入etree报错的解决方案
Jun 26 Python
详解从Django Allauth中进行登录改造小结
Dec 18 Python
Python实现ATM系统
Feb 17 Python
Python使用QQ邮箱发送邮件实例与QQ邮箱设置详解
Feb 18 Python
在python3中使用shuffle函数要注意的地方
Feb 28 Python
解决pycharm安装第三方库失败的问题
May 09 Python
Django微信小程序后台开发教程的实现
Jun 03 Python
pyCharm 设置调试输出窗口中文显示方式(字符码转换)
Jun 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
ThinkPHP关联模型操作实例分析
2012/09/23 PHP
php cc攻击代码与防范方法
2012/10/18 PHP
PHP的foreach中使用引用时需要注意的一个问题和解决方法
2014/05/29 PHP
destoon各类调用汇总
2014/06/20 PHP
ThinkPHP3.1新特性之动态设置自动完成及自动验证示例代码
2014/06/23 PHP
PHP文件缓存内容保存格式实例分析
2014/08/20 PHP
php使用数组填充下拉列表框的方法
2015/03/31 PHP
用javascript获取textarea中的光标位置
2008/05/06 Javascript
javascript+xml技术实现分页浏览
2008/07/27 Javascript
JavaScript中的style.display属性操作
2013/03/27 Javascript
常见的jQuery选择器汇总
2014/11/24 Javascript
详解js跨域原理以及2种解决方案
2015/12/09 Javascript
javascript弹出带文字信息的提示框效果
2016/07/19 Javascript
AngularJS通过$location获取及改变当前页面的URL
2016/09/23 Javascript
原生js开发的日历插件
2017/02/04 Javascript
node.js爬虫爬取拉勾网职位信息
2017/03/14 Javascript
JS实现的将html转为pdf功能【基于浏览器端插件jsPDF】
2018/02/06 Javascript
如何利用nodejs实现命令行游戏
2020/11/24 NodeJs
Python学习笔记(一)(基础入门之环境搭建)
2014/06/05 Python
qpython3 读取安卓lastpass Cookies
2016/06/19 Python
Python实现列表删除重复元素的三种常用方法分析
2017/11/24 Python
python按键按住不放持续响应的实例代码
2019/07/17 Python
pytorch::Dataloader中的迭代器和生成器应用详解
2020/01/03 Python
解决python3中os.popen()出错的问题
2020/11/19 Python
La Senza官网:北美顶尖性感内衣品牌
2018/08/03 全球购物
俄罗斯第一家篮球店:StreetBall
2020/07/30 全球购物
餐饮加盟计划书
2014/01/10 职场文书
统计员岗位职责
2015/02/11 职场文书
村官个人总结范文
2015/03/03 职场文书
清洁工个人总结
2015/03/04 职场文书
年度考核表个人总结
2015/03/06 职场文书
实施意见格式范本
2015/06/05 职场文书
同意转租证明
2015/06/24 职场文书
党员转正大会主持词
2015/07/02 职场文书
cf战队宣传语
2015/07/13 职场文书
opencv深入浅出了解机器学习和深度学习
2022/03/17 Python