python配置模块-ConfigParser

ConfigParser 是用来读取配置文件的包。配置文件的格式如下:中括号“[ ]”内包含的为section。section 下面为类似于key-value 的配置内容。

[sec_a]
; a comment
a_key1 = 20
a_key2 = 10

[sec_b]
b_key1 = 121
b_key2 = b_value2
b_key3 = $r
b_key4 = 127.0.0.1

其中, 中括号“[ ]”内包含的为section。紧接着section 为类似于key-value 的options 的配置内容

模块安装:

sudo pip install configparser

主要方法:

读取配置文件

read(filename) 直接读取ini文件内容
sections() 得到所有的section,并以列表的形式返回
options(section) 得到该section的所有option
items(section) 得到该section的所有键值对
get(section,option) 得到section中option的值,返回为string类型
getint(section,option) 得到section中option的值,返回为int类型
写入配置文件

add_section(section) 添加一个新的section
set( section, option, value) 对section中的option进行设置需要调用write将内容写入配置文件。
ConfigParser 初始工作
使用ConfigParser 首选需要初始化实例,加载并读取配置文件:

import ConfigParser
cfg = ConfigParser.ConfigParser()
cfg.read(‘test.cfg’) # 配置文件名

ConfigParser 常用方法
获取所有sections。也就是将配置文件中所有“[ ]”读取到列表中:

s = cfg.sections()
print s

将输出(以下将均以简介中配置文件为例):
[‘sec_a’, ‘sec_b’]

获取指定section 的options。即将配置文件某个section 内key 读取到列表中:

opts = cfg.options(‘sec_a’)
print opts

输出:
[‘a_key1’, ‘a_key2’]

获取指定section 的配置信息。

v = cfg.items(‘sec_a’)

输出:
[(‘a_key1’, ‘20’), (‘a_key2’, ‘10’)]

按照类型读取指定section 的option 信息。
同样的还有getfloat、getboolean。

#可以按照类型读取出来

直接取 sec_a中值

a_key1 = cfg.get(‘sec_a’, ‘a_key1’)
a_key2 = cfg.getint(‘sec_a’, ‘a_key2’)

直接取 sec_b中值

b_key1 = cfg.getint(‘sec_b’, ‘b_key1’)
b_key2 = cfg.get(‘sec_b’, ‘b_key2’)
b_key3 = cfg.get(‘sec_b’, ‘b_key3’)
b_key4 = cfg.get(‘sec_b’, ‘b_key4’)

print ‘a_key1’, type(a_key1), a_key1
print ‘a_key2’, type(a_key2), a_key2

print ‘b_key1’, type(b_key1), b_key1
print ‘b_key2’, type(b_key2), b_key2
print ‘b_key3’, type(b_key3), b_key3
print ‘b_key4’, type(b_key4), b_key4

将输出:

a_key1 <type ‘str’> 20
a_key2 <type ‘int’> 10

b_key1 <type ‘int’> 121
b_key2 <type ‘str’> b_value2
b_key3 <type ‘str’> $r
b_key4 <type ‘str’> 127.0.0.1

设置某个option 的值。(记得最后要写回)

cfg.set(‘sec_a’, ‘a_key3’, ‘PSP’)
cfg.write(open(‘test.conf’, ‘w’))

添加一个section。(同样要写回)

cfg.add_section(‘sec_c’)
cfg.set(‘sec_c’, ‘int’, 15)
cfg.set(‘sec_c’, ‘bool’, True)
cfg.set(‘sec_c’, ‘float’, 3.1415)
cfg.set(‘sec_c’, ‘baz’, ‘fun’)
cfg.write(open(‘test.conf’, ‘w’))

移除section 或者option 。(只要进行了修改就要写回的哦)

cfg.remove_option(‘sec_a’, ‘a_key1’)
cfg.remove_section(‘sec_b’)
cfg.write(open(‘test.conf’, ‘w’))
1
2
3
最后附上示例中用到的完整代码:

import ConfigParser
cfg = ConfigParser.ConfigParser()
cfg.read(‘test.cfg’)

s = cfg.sections()
print s

opts = cfg.options(‘sec_a’)
print opts

v = cfg.items(‘sec_a’)
print v

a_key1 = cfg.get(‘sec_a’, ‘a_key1’)
a_key2 = cfg.getint(‘sec_a’, ‘a_key2’)

b_key1 = cfg.getint(‘sec_b’, ‘b_key1’)
b_key2 = cfg.get(‘sec_b’, ‘b_key2’)
b_key3 = cfg.get(‘sec_b’, ‘b_key3’)
b_key4 = cfg.get(‘sec_b’, ‘b_key4’)

print ‘a_key1’, type(a_key1), a_key1
print ‘a_key2’, type(a_key2), a_key2

print ‘b_key1’, type(b_key1), b_key1
print ‘b_key2’, type(b_key2), b_key2
print ‘b_key3’, type(b_key3), b_key3
print ‘b_key4’, type(b_key4), b_key4

cfg.set(‘sec_a’, ‘a_key3’, ‘PSP’)
cfg.add_section(‘sec_c’)
cfg.set(‘sec_c’, ‘int’, 15)
cfg.set(‘sec_c’, ‘bool’, True)
cfg.set(‘sec_c’, ‘float’, 3.1415)
cfg.set(‘sec_c’, ‘baz’, ‘fun’)

cfg.remove_option(‘sec_a’, ‘a_key1’)
cfg.remove_section(‘sec_b’)
cfg.write(open(‘test.conf’, ‘w’))

补充:

#!/usr/bin/env python
import ConfigParser
import sys
config=ConfigParser.ConfigParser()
config.add_section(“book1”)
config.set(“book1”,“title”,“hello world”)
config.set(“book1”,“aut”,“log”)
config.write(open(“f.txt”,“w”))

转载自:https://blog.csdn.net/u010472607/article/details/77338308 如有问题,请联系删除

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值