$用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)
随机推荐
- Attention Mechanism
首先介绍Attention机制: 转自:http://blog.csdn.net/malefactor/article/details/50550211 上面讲的是Soft Attention Mod ...
- 应用程序无法正常启动 0x0000005
FeiQ应用程序无法正常启动了,错误代码0x0000005 右键FeiQ.exe,[属性],以Windows7兼容模式运行~
- CNBlog客户端--第二阶段记录
开始 先给大家看一下我最近的进度,由于最近事比较多,所以这块的精力就相对较少了!但是还是有成绩的!!大家先看效果图吧! 这个优化之后的博客内容显示,还有增加了评论显示页面!! 这个是设置页面,还有一些 ...
- Pycharm 2017 12月最新激活码
激活的办法:这个必须的联网才可以使用(每次打开PyCharm都需要电脑联网才可以正常使用),要是没网的话,就不能激活使用啦,大家注意哈. http://idea.iteblog.com/key.php ...
- JAVA基础面试(四4)
31.String s = new String("xyz");创建了几个StringObject?是否可以继承String类? 两个或一个都有可能,”xyz”对应一个对象,这个对 ...
- script跨域之360搜索
思考: 布局: 1,flex元素上下左右居中,内部元素横向排列: div{ /* 100vh = viewport height*/ display: flex; justify-content: c ...
- Webservice工作原理及实例
Web Service工作原理及实例 一.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者In ...
- Eclipse出现ContextLoaderListener not find
严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderLis ...
- [LintCode] 全排列
递归实现: class Solution { public: /** * @param nums: A list of integers. * @return: A list of permutati ...
- 【IDEA】Maven踩坑:pom文件中的默认profiles不生效+IDEA中Maven的profiles使用说明
一.问题即分析 项目pom文件中的profiles有3个配置:dev.test和production 默认配置的是dev,如下图: 但在本地起服务时,读取的配置始终是test里的. 二.原因 2.1 ...