$用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)
随机推荐
- python笔记6:常用模块
模块,模块就是封装了特殊功能的代码. 模块分为三种: 自定义模块 第三方模块 内置模块 1.自定义模块 自定义模块就是自己定义的模块,如何import自定义模块,如下: (1)主程序与模块程序在同一目 ...
- 【BZOJ】3404: [Usaco2009 Open]Cow Digit Game又见数字游戏(博弈论)
http://www.lydsy.com/JudgeOnline/problem.php?id=3404 写挫好几次.... 裸的博弈论即可.. #include <cstdio> #in ...
- Autorotation and Autosizing
配置应用级别的旋转方向——Global Setting 方法:点击项目-General-Deployment-Device Orientation It doesn’t necessarily mea ...
- flask渲染模板
Flask自身使用了jinja2模板,可以使用render_template()方法来渲染模板,只需要将模板名和关键字的参数传入. 该渲染模板的模块(views.py)会在 templates 文件夹 ...
- LNK2005 _DllMain@12 mfcs100d.lib
起因是将之前使用 MFC 规则 DLL 的动态库都改为了 MFC 扩展 DLL,在将动态库中从 CWinApp 继承的类替换为 DllMain 函数后,就出现 LNK2005 错误,说 DllMain ...
- Django - 环境搭建、url、视图、模板、标签、过滤器
(一).简介 简介就不多说了,网上的内容一大堆.总结来说,django是走大而全的路线,写项目超级快,几乎什么都为你考虑到了,你就乖乖照着它的格式来写就行了. 这里来一些基本认知: web应用框架(w ...
- Web的本质以及第一个Django实例.
Web框架的本质: 所有的Web应用本质上就是一个socket服务器, 而用户的浏览器就是一个socket客户端. import socket sk = socket.socket() s ...
- 【BZOJ4384】[POI2015]Trzy wieże 树状数组
[BZOJ4384][POI2015]Trzy wieże Description 给定一个长度为n的仅包含'B'.'C'.'S'三种字符的字符串,请找到最长的一段连续子串,使得这一段要么只有一种字符 ...
- C#自动给文章关键字加链接实现代码
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...
- js parseInt()函数中的问题。。
今天在看<javascript 高级程序设计>时, 与我的输出结果不符合, <!DOCTYPE html> <html lang="en"> & ...