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

 

posted on 2016-06-03 11:35  Alex0425  阅读(190)  评论(0编辑  收藏  举报