您的位置:首页 > 编程语言 > Python开发

Python configparser模块

2016-06-03 11:35 375 查看
import configparser

conf = configparser.ConfigParser()
conf.read("test.cfg")
sections = conf.sections()
print (sections)

['sec_c', 'sec_b', 'sec_a']

options = conf.options("sec_a")
print (options)
kvs = conf.items("sec_a")
print(kvs)

['a_key1', 'a_key2']

str_val = conf.get("sec_a","a_key1")
print (type(str_val))
print (str_val)

<class 'str'>
20

int_val = conf.getint("sec_a","a_key2")
print (type(int_val))
print (int_val)

<class 'int'>
10

conf.set("sec_b","b_key3","$new-$r")
conf.write(open("test.cfg","w"))

修改[sec_a]段中[b_key3]的值为"$new-$r"

conf.set("sec_b","b_newkey","new_value")
conf.write(open("test.cfg","w"))
在[sec_b]中先加了一个option

conf.add_section("sec_d")
conf.set("sec_d","new_d_key","new_d_value")
conf.write(open("test.cfg","w"))
新增加一个section
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: