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)

随机推荐

  1. mfc小工具开发之定时闹钟之---多线程急线程同步

    一.MFC对多线程编程的支持 MFC中有两类线程,分别称之为工作者线程和用户界面线程.二者的主要区别在于工作者线程没有消息循环,而用户界面线程有自己的消息队列和消息循环. 工作者线程没有消息机制,通常 ...

  2. 【BZOJ】1687: [Usaco2005 Open]Navigating the City 城市交通(bfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1687 bfs后然后逆向找图即可.因为题目保证最短路唯一 #include <cstdio> ...

  3. Objective-C中的instancetype和id关键字(转)

    转自:Objective-C中的instancetype和id关键字 一.什么是instancetype 同id一样,都是表示未知类型的的对象. 二.关联返回类型(related result typ ...

  4. 应用程序无法正常启动 0x0000005

    FeiQ应用程序无法正常启动了,错误代码0x0000005 右键FeiQ.exe,[属性],以Windows7兼容模式运行~    

  5. 学习-go语言坑之for range

    引用自 http://studygolang.com/articles/9701 go只提供了一种循环方式,即for循环,在使用时可以像c那样使用,也可以通过for range方式遍历容器类型如数组. ...

  6. cxGrid 使用指南 1

    1:cxgrid是应该数据关联的控件,类似dbgrid.2:一般用来查阅表信息,如果要修改的话,直接在上面编辑或添加 非常不方便通常要放几个EDit来对选中的记录进行编辑或添加记录. 因为表一般都有主 ...

  7. Blue Bird

    Blue Bird 哈巴他一 他拉 毛套拉那 一套一太(他)卖咋西他 闹哇 啊哦一 啊哦一 啊闹扫啦 卡那西米哇马达 哦包爱 啦来字赛次那撒哇姨妈 次卡米哈几卖他阿娜塔爱套一大 靠闹看叫毛姨妈靠逃吧你 ...

  8. [LintCode] 两个排序数组的中位数

    class Solution { public: /** * @param A: An integer array. * @param B: An integer array. * @return: ...

  9. 微软MVP Round Table

    2017年7月7日,微软VS圈子的老大兼女神Julia(潘正磊)以及Peter Hu等人,和若干MVP一起在进行了一次Round Table讨论. 讨论过程中主要针对VS和TFS/VSTS相关的功能. ...

  10. Net应用架构设计

    N-Tier 是从架构更大的维度上划分,每一个维度都是一个Tier(在微软的ESP2.0里翻译为”级”),比如电商架构划分如下: UI 服务接口 消息.缓存中间件 数据库 ...... Tier与Ti ...