$用ConfigParser模块读写conf配置文件
ConfigParser是Python内置的一个读取配置文件的模块,用它来读取和修改配置文件非常方便,本文介绍一下它的基本用法。
数据准备
假设当前目录下有一个名为sys.conf
的配置文件,其内容如下:
[db]
db_host=127.0.0.1
db_port=22
db_user=root
db_pass=root123
[concurrent]
thread = 10
processor = 20
注:配置文件中,各个配置项其实是用等号'='隔开的键值对,这个等号两边如果有空白符,在处理的时候都会被自动去掉。但是key之前不能存在空白符,否则会报错。
配置文件介绍
配置文件即conf文件,其文件结构多为键值对的文件结构,比如上面的sys.conf文件。
conf文件有2个层次结构,[]
中的文本是section的名称,下面的键值对列表是item,代表每个配置项的键和值。
初始化ConfigParser实例
import ConfigParser
cf = ConfigParser.ConfigParser()
cf.read('./sys.conf')
读取所有的section列表
section即[]
中的内容。
s = cf.sections()
print '【Output】'
print s
【Output】
['db', 'concurrent']
读取指定section下options key列表
options即某个section下的每个键值对的key.
opt = cf.options('concurrent')
print '【Output】'
print opt
【Output】
['thread', 'processor']
获取指定section下的键值对字典列表
items = cf.items('concurrent')
print '【Output】'
print items
【Output】
[('thread', '10'), ('processor', '20')]
按照指定数据类型读取配置值
cf对象有get()、getint()、getboolean()、getfloat()四种方法来读取不同数据类型的配置项的值。
db_host = cf.get('db','db_host')
db_port = cf.getint('db','db_port')
thread = cf.getint('concurrent','thread')
print '【Output】'
print db_host,db_port,thread
【Output】
127.0.0.1 22 10
修改某个配置项的值
比如要修改一下数据库的密码,可以这样修改:
cf.set('db','db_pass','newpass')
# 修改完了要写入才能生效
with open('sys.conf','w') as f:
cf.write(f)
添加一个section
cf.add_section('log')
cf.set('log','name','mylog.log')
cf.set('log','num',100)
cf.set('log','size',10.55)
cf.set('log','auto_save',True)
cf.set('log','info','%(bar)s is %(baz)s!')
# 同样的,要写入才能生效
with open('sys.conf','w') as f:
cf.write(f)
执行上面代码后,sys.conf文件多了一个section,内容如下:
[log]
name = mylog.log
num = 100
size = 10.55
auto_save = True
info = %(bar)s is %(baz)s!
移除某个section
cf.remove_section('log')
# 同样的,要写入才能生效
with open('sys.conf','w') as f:
cf.write(f)
移除某个option
cf.remove_option('db','db_pass')
# 同样的,要写入才能生效
with open('sys.conf','w') as f:
cf.write(f)
随机推荐
- 如何将HTML页面的标题设置为“数字天堂”。
如何将HTML页面的标题设置为“数字天堂”. 解答: <html> <head><title>数字天堂</title></head> < ...
- Java中带包的类的编译与执行
http://blog.csdn.net/wbrs13/article/details/4859880
- linux中的etc目录
etc不是什么缩写,是and so on的意思 来源于 法语的 et cetera 翻译成中文就是 等等 的意思. 至于为什么在/etc下面存放配置文件, 按照原始的UNIX的说法(linux文件结构 ...
- oracle批量update
我个人觉得写的很好 http://blog.csdn.net/wanglilin/article/details/7200201 需求: 将t2(t_statbuf)表中id和t1(T_Mt)表相同的 ...
- 第四课(2)——mysql配置参数讲解
*****************general***************** user 启动mysql domain的用户 port 数据库端口号 socket 数据库socket文件的路径 p ...
- mysql出现) Notice: Trying to get property of non-object in E:\p错误的 原因
在mysql中,每个命令之间都要留一点空格 如果是这样, $query = "select * from books where " .$searchtype. "lik ...
- python将图片转base64,前端显示
https://blog.csdn.net/u013055678/article/details/71406746 <img src='xxxxxxx'> 跟这样的效果是一样的吧? ...
- Properties 集合
Map Hashtable Properties 特点: 该集合中的键和值都是字符串类型 集合中的数据可以保存到流中, 或者从流中获取 应用: 通常该集合用于操作以键值对形式存在的配置文件 常用方法: ...
- 转!java web项目 build path 导入jar包,tomcat启动报错 找不到该类
在eclipse集成tomcat开发java web项目时,引入的外部jar包,编译通过,但启动tomcat运行web时提示找不到jar包内的类,需要作如下配置,将jar包在部署到集成的tomcat环 ...
- Tickets---hdu1260(简单dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1260 题意是有n个人排队买票,第 i 个人买票所需要的时间是a[i],这个人和 i-1 或者 i+1 ...