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创建线程示例
May 06 Python
Python中的ceil()方法使用教程
May 14 Python
详解Python中的四种队列
May 21 Python
详解python3中tkinter知识点
Jun 21 Python
Django实现分页功能
Jul 02 Python
解决vscode python print 输出窗口中文乱码的问题
Dec 03 Python
Python基于百度云文字识别API
Dec 13 Python
Python+OpenCV图片局部区域像素值处理详解
Jan 23 Python
Python 中pandas索引切片读取数据缺失数据处理问题
Oct 09 Python
Python如何实现强制数据类型转换
Nov 22 Python
python 等差数列末项计算方式
May 03 Python
python raise的基本使用
Sep 10 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日历[测试通过]
2008/03/27 PHP
windows的文件系统机制引发的PHP路径爆破问题分析
2014/07/28 PHP
ThinkPHP框架搭建及常见问题(XAMPP安装失败、Apache/MySQL启动失败)
2016/04/15 PHP
详细解读php的命名空间(二)
2018/02/21 PHP
Yii2结合Workerman的websocket示例详解
2018/09/10 PHP
win10下 php安装seaslog扩展的详细步骤
2020/12/04 PHP
JS网页播放声音实现代码兼容各种浏览器
2013/09/22 Javascript
jQuery简单实现日历的方法
2015/05/04 Javascript
js基础知识(公有方法、私有方法、特权方法)
2015/11/06 Javascript
jQuery实现鼠标选中文字后弹出提示窗口效果【附demo源码】
2016/09/05 Javascript
AngularJS中如何使用echart插件示例详解
2016/10/26 Javascript
Bootstrap布局之栅格系统学习笔记
2017/05/04 Javascript
详解AngularJS1.6版本中ui-router路由中/#!/的解决方法
2017/05/22 Javascript
JS获取字符对应的ASCII码实例
2017/09/10 Javascript
微信小程序实现通过双向滑动缩放图片大小的方法
2018/12/30 Javascript
javascript实现简单留言板案例
2021/02/09 Javascript
Python2.x版本中cmp()方法的使用教程
2015/05/14 Python
Python3控制路由器——使用requests重启极路由.py
2016/05/11 Python
Python selenium文件上传方法汇总
2020/11/19 Python
Python设计模式之工厂模式简单示例
2018/01/09 Python
Tensorflow卷积神经网络实例进阶
2018/05/24 Python
解决pandas .to_excel不覆盖已有sheet的问题
2018/12/10 Python
Python实现的序列化和反序列化二叉树算法示例
2019/03/02 Python
浅谈Selenium 控制浏览器的常用方法
2020/12/04 Python
斯洛伐克家具和时尚装饰品购物网站:Butlers.sk
2019/09/08 全球购物
美国美食礼品篮网站:Gourmet Gift Baskets
2019/12/15 全球购物
AJax面试题
2014/11/25 面试题
自荐信格式的六要素
2013/09/21 职场文书
科室工作的个人自我评价
2013/10/30 职场文书
迎新春趣味活动方案
2014/08/24 职场文书
学习优秀共产党员先进事迹思想报告
2014/09/17 职场文书
党的群众路线教育实践活动制度建设计划
2014/11/03 职场文书
个人先进事迹材料范文
2014/12/29 职场文书
拉贝日记观后感
2015/06/05 职场文书
小学二年级班主任工作经验交流材料
2015/11/02 职场文书
SQL实现LeetCode(180.连续的数字)
2021/08/04 MySQL