Python用ConfigParser解析INI配置文件
2012-10-19 13:04
751 查看
在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是ConfigParser,这里简单的做一些介绍。
常见INI配置文件如下:
常见INI配置文件如下:
[database] db_host = 127.0.0.1 db_name = mydatabases db_user = root db_pwd = db_charset = utf-8 db_pconnect = 1 [Main] timeout = 30 rootdir = d:\python\configTest cachedir = d:\python\caches
# coding:utf-8 import ConfigParser cf = ConfigParser.ConfigParser() cf.read("config.ini") #返回所有的section print cf.sections() #返回所有option print cf.options('database') #取出数据 print cf.get('database','db_name') print cf.getboolean('database','db_pconnect') print cf.getint('Main','timeout') #返回节点列表 print cf.items('Main') #添加配置节点 if not cf.has_section('members'): cf.add_section('members') cf.set('members','autologin','YES') print cf.sections() #保存配置参数到配置文件 cf.write( open("config.ini",'w') )
相关文章推荐
- python开发_configparser_解析.ini配置文件工具_完整版_博主推荐
- python ConfigParser – 配置文件解析(转载)
- python中用Configparser模块解析配置文件
- python ConfigParser模块 配置文件解析
- Python配置文件解析模块ConfigParser使用实例
- 在python中实现自定义配置文件解析及应用技巧[yaml and ConfigParser] 推荐
- python模块之ConfigParser: 用python解析配置文件[zt]
- Python 解析ini文件 By ConfigParser
- Python使用自带的ConfigParser模块读写ini配置文件
- Python使用自带的ConfigParser模块读写ini配置文件
- python模块之ConfigParser: 用python解析配置文件
- python模块之ConfigParser: 用python解析配置文件
- python模块之ConfigParser:解析配置文件模块
- ConfigParser模块学习: 用python解析配置文件
- python 中对配置文件的解析 ConfigParser & ConfigObj
- python 中对配置文件的解析 ConfigParser & ConfigObj
- python ConfigParser – 配置文件解析
- [Python]解析.ini 文件出错ConfigParser.MissingSectionHeaderError: File contains no section headers.
- [zz]python模块之ConfigParser: 用python解析配置文件
- Python模块configparser(操作配置文件ini)