Python 解析ini文件 By ConfigParser
2014-08-29 21:36
471 查看
Python 解析ini文件 By ConfigParser
ini文件是windows中经常使用的配置文件,主要的格式为:
python提供了一个简单的模块
ConfigParser.RawConfigParser()
RawConfigParser Object的操作有:
.sections() : 返回所有可用的section
.addsection(sectionname) :添加section
.set(sectionname, optionname, optionvalue): 添加option
.hassection(sectionname) :判断
.options(sectionname) : 返回section下可用的option
.hasoption(sectionname, optionname) : 判断
.read(filename) : 读取文件
.wrie(filename) : 将RawConfigParser对象写到文件中
.get(sectionname, optionname) : 获取值, 默认的是返回string类型
.getfloat, .getint, .getboolean : 获取不同类型的返回值,参数和get的参数一样
.items(sectionname) :列出section下的所有key:value
.remove(sectionname) :删除section
.remove(sectionname, option_name) : 删除section下的某个option
Demo -- 生成文件
最后生成的文件为:
Demo -- 读取文件
最后的结果为:
ini文件是windows中经常使用的配置文件,主要的格式为:
[Section1] option1 : value1 option2 : value2
python提供了一个简单的模块
ConfigParser可以用来解析类似这种形式的文件。对于
ConfigParser模块可以解析
key:value和
key=value这样的类型,对于
#和
;开头的行将会自动忽视掉。相当于注释行。常用的函数:
ConfigParser.RawConfigParser()
RawConfigParser Object的操作有:
.sections() : 返回所有可用的section
.addsection(sectionname) :添加section
.set(sectionname, optionname, optionvalue): 添加option
.hassection(sectionname) :判断
.options(sectionname) : 返回section下可用的option
.hasoption(sectionname, optionname) : 判断
.read(filename) : 读取文件
.wrie(filename) : 将RawConfigParser对象写到文件中
.get(sectionname, optionname) : 获取值, 默认的是返回string类型
.getfloat, .getint, .getboolean : 获取不同类型的返回值,参数和get的参数一样
.items(sectionname) :列出section下的所有key:value
.remove(sectionname) :删除section
.remove(sectionname, option_name) : 删除section下的某个option
Demo -- 生成文件
$ cat ini_demo.py # -*- coding:utf-8 -*- import ConfigParser def gen_ini(): ftest = open('test','w') config_write = ConfigParser.RawConfigParser() config_write.add_section('Section_a') config_write.add_section('Section_b') config_write.add_section('Section_c') config_write.set('Section_a','option_a1','apple_a1') config_write.set('Section_a','option_a2','banana_a2') config_write.set('Section_b','option_b1','apple_b1') config_write.set('Section_b','option_b2','banana_b2') config_write.set('Section_c','option_c1','apple_c1') config_write.set('Section_c','option_c2','banana_c2') config_write.write(ftest) ftest.close() if __name__ == "__main__": gen_ini()
最后生成的文件为:
$ cat test [Section_a] option_a1 = apple_a1 option_a2 = banana_a2 [Section_c] option_c2 = banana_c2 option_c1 = apple_c1 [Section_b] option_b1 = apple_b1 option_b2 = banana_b2
Demo -- 读取文件
def read_ini(): config_read = ConfigParser.RawConfigParser() config_read.read('test') print config_read.sections() print config_read.items('Section_a') print config_read.get('Section_a','option_a1')
最后的结果为:
['Section_a', 'Section_c', 'Section_b'] [('option_a2', 'banana_a2'), ('option_a1', 'apple_a1')] apple_a1
相关文章推荐
- [Python]解析.ini 文件出错ConfigParser.MissingSectionHeaderError: File contains no section headers.
- python开发_configparser_解析.ini配置文件工具_完整版_博主推荐
- Python用ConfigParser解析INI配置文件
- python 中对配置文件的解析 ConfigParser & ConfigObj
- python模块之ConfigParser: 用python解析配置文件
- 在python中实现自定义配置文件解析及应用技巧[yaml and ConfigParser] 推荐
- 【Python】配置文件解析模块ConfigParser使用实例
- Python配置文件解析模块ConfigParser使用实例
- ConfigParser模块学习: 用python解析配置文件
- Python使用自带的ConfigParser模块读写ini配置文件
- Python模块configparser(操作配置文件ini)
- python ConfigParser – 配置文件解析
- Python使用自带的ConfigParser模块读写ini配置文件
- python ConfigParser模块 配置文件解析
- python模块之ConfigParser:解析配置文件模块
- python中用Configparser模块解析配置文件
- python模块之ConfigParser: 用python解析配置文件[zt]
- python 中对配置文件的解析 ConfigParser & ConfigObj
- python模块之ConfigParser: 用python解析配置文件
- [zz]python模块之ConfigParser: 用python解析配置文件