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
相关文章推荐
- python urllib2
- python爬虫基础A
- python中的@符号的作用
- python 基本语法
- Python NLTK学习
- PYTHON 自定义异常类的使用
- Python的静态方法和类方法有什么区别
- Python 输出文字带颜色
- 命令形式新建第一个python 项目
- Python函数式练习
- 小强IT游记之大连行
- adaboost算法
- 小强IT游记之厦门行
- Python常用模块之sys
- python内置函数每个执行一次
- python 2与3的区别汇总
- python GIL 全局锁
- python实用笔记,加快编程速度,lamdba,三元运算,open.
- numpy读写文件
- eclipse 搭建 python 开发环境