Python使用自带的ConfigParser模块读写ini配置文件
2016-06-26 17:48
1166 查看
在用Python做开发的时候经常会用到数据库或者其他需要动态配置的东西,硬编码在里面每次去改会很麻烦。Python自带有读取配置文件的模块ConfigParser,使用起来非常方便。
ini文件
ini配置文件格式:
读取配置文件:
import ConfigParser conf = ConfigParser.ConfigParser() conf.read('dbconf.ini') # 文件路径 name = conf.get("section1", "name") # 获取指定section 的option值 print name sex = conf.get("section1", "sex") # 获取section1 的sex值 print age
输出:
jhao male
写入配置文件:
import ConfigParser conf = ConfigParser.ConfigParser() conf.read('dbconf.ini') conf.set("section1", "name", "jhao104") # 修改指定section 的option conf.set("section1", "age", "21") # 增加指定section 的option conf.add_section("section3") # 增加section conf.set("section3", "site", "oschina.net") # 给新增的section 写入option conf.write(open('dbconf.ini', 'w'))
输出:
您可能感兴趣的文章:
- Python自动化测试ConfigParser模块读写配置文件
- Python中的ConfigParser模块使用详解
- Python内置模块ConfigParser实现配置读写功能的方法
- 详解Python读取配置文件模块ConfigParser
- Python配置文件解析模块ConfigParser使用实例
- Python中使用ConfigParser解析ini配置文件实例
- python解析模块(ConfigParser)使用方法
- Python实现读写INI配置文件的方法示例
- Python实现加载及解析properties配置文件的方法
- Python使用ConfigParser模块操作配置文件的方法
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法