码迷,mamicode.com
首页 > 编程语言 > 详细

Python configparser模块

时间:2016-06-03 12:57:20      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

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

 

Python configparser模块

标签:

原文地址:http://www.cnblogs.com/python-study/p/5555727.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!