configparser模块

configparse

configparser用于处理特定格式的文件,其本质上是利用open来操作文件

1.1.读取配置文件

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

1.2.写入配置文件

-add_section(section) 添加一个新的section
-set( section, option, value) 对section中的option进行设置
  需要调用write将内容写入配置文件。

2,测试实例

2.1,测试1

配置文件test.cfg

1 [sec_a]  
2 a_key1 = 20  
3 a_key2 = 10  
4   
5 [sec_b]  
6 b_key1 = 121  
7 b_key2 = b_value2  
8 b_key3 = $r  
9 b_key4 = 127.0.0.1 

测试文件test.py

import configparser
#生成config对象
config=configparser.ConfigParser()
#用config对象读取配置文件
config.read('text.cfg')

##以列表形式返回所有的section(节点)
sections=config.sections()
print(sections)

###['sec_a', 'sec_b']

#得到指定section的所有option(指定的节点下的所有key)
options=config.options('sec_a')
print(options)

##['a_key1', 'a_key2']

## 得到指定section(节点)的所有键值对
kvs=config.items('sec_a')
print(kvs)

## [('a_key1', '20'), ('a_key2', '10')]

##指定section(节点)option(key)读取值
str_val=config.get('sec_a','a_key1')
int_val = config.getint('sec_a','a_key2')

print ( str_val)
print ( int_val)

## 20
## 10


#写配置文件
#更新指定section,option的值
config.set('sec_b','b_key3','enw-$r1')

##写入指定section增加新option和值
config.set("sec_b", "b_newkey", "new-value")

##增加新的section
config.add_section('a_anew_section')
config.set('a_anew_section','new_key','new_value')
##写回配置文件
config.write(open('test.cfg','w'))

补充

配置文件wok.cfg

[mysql]
db_host = 121
db_port = 3306
db_user = root
db_pass = password

[个人信息]
name = 飞飞
age = 30
address = 重庆沙坪坝
tel = 13900000000

[add]
add1 = 添加字符串
add2 = 再添加字符串

[del]
del1 = 删除字符串

[addd]
addd1 = addd1的值
addd2 = addd2的值

 

 

## 、检查、删除、添加节点
import configparser
config=configparser.ConfigParser()
config.read('wok',encoding='utf-8')

#检查,检测到这个节点返回True没有检查到返回False
hasr= config.has_section('mysql')
print(hasr)

#添加节点
config.add_section("SEC_1")
config.write(open('wok', 'w'))

#删除节点
config.remove_section("SEC_1")
config.write(open('wok', 'w'))

#检查、删除、设置指定组内的键值对
import configparser

config = configparser.ConfigParser()
config.read('wok', encoding='utf-8')

# 检查
has_opt = config.has_option('mysql', 'db_host')
print(has_opt)

# 删除
config.remove_option('mysql', 'db_host')
config.write(open('wok', 'w'))

设置  修改值
config.set('mysql', 'db_host', "121")
config.write(open('wok', 'w'))

 

 

 

2.2,测试2

Python的ConfigParser Module中定义了3个类对INI文件进行操作。

分别是RawConfigParser、ConfigParser、SafeConfigParser。

RawCnfigParser是最基础的INI文件读取类,ConfigParser、SafeConfigParser支持对%(value)s变量的解析。 

配置文件test.cfg

[portal]  
url = http://%(host)s:%(port)s/Portal  
host = localhost  
port = 8080 

使用RawConfigParser:

# import configparser
# config = configparser.RawConfigParser()##生成一个config读取对象
# config.read('text.cfg')##读取这个文件
# print(config.get('portal','url'))
#
# config.set('portal','ur12','%(host)s:%(port)s')##添加属性
# print(config.get('portal','ur12'))
# config.write(open("test.cfg", "w"))####重新写入文件
输出
# ## http://%(host)s:%(port)s/Portal
# ## %(host)s:%(port)s

ConfigParser

import configparser
config =configparser.ConfigParser()
config.read('text.cfg')
print(config.get('portal','url'))

config.set('portal','url2',"%(host)s:%(port)s")
print(config.get('portal','url2'))
config.write(open('text.cfg','w'))

输出结果

http://local:8080/Portal
local:8080

改用SafeConfigParser,效果与ConfigParser相同

 

 

结论:

还是用ConfigParser

 

posted @ 2016-05-26 11:14  青春永不言弃  阅读(215)  评论(0)    收藏  举报