python解析模块(ConfigParser)使用方法
2013-12-10 09:39
1116 查看
测试配置文件test.conf内容如下:
复制代码 代码如下:[first]
w = 2
v: 3
c =11-3
[second]
sw=4
test: hello
测试配置文件中有两个区域,first和second,另外故意添加一些空格、换行。
下面解析:
复制代码 代码如下:>>> import ConfigParser
>>> conf=ConfigParser.ConfigParser()
>>> conf.read('test.conf')
['test.conf']
>>> conf.sections() #获得所有区域
['first', 'second']
>>> for sn in conf.sections():
... print conf.options(sn) #打印出每个区域的所有属性
...
['w', 'v', 'c']
['sw', 'test']
获得每个区域的属性值:
复制代码 代码如下:for sn in conf.sections():
print sn,'-->'
for attr in conf.options(sn):
print attr,'=',conf.get(sn,attr)
输出:
复制代码 代码如下:first -->
w = 2
v = 3
c = 11-3
second -->
sw = 4
test = hello
好了,以上就是基本的使用过程,下面是动态的写入配置,
复制代码 代码如下:cfd=open('test2.ini','w')
conf=ConfigParser.ConfigParser()
conf.add_section('test') #add a section
conf.set('test','run','false')
conf.set('test','set',1)
conf.write(cfd)
cfd.close()
上面是向test2.ini写入配置信息。
您可能感兴趣的文章:
- Python自动化测试ConfigParser模块读写配置文件
- Python使用自带的ConfigParser模块读写ini配置文件
- Python中的ConfigParser模块使用详解
- Python内置模块ConfigParser实现配置读写功能的方法
- 详解Python读取配置文件模块ConfigParser
- Python配置文件解析模块ConfigParser使用实例
- Python中使用ConfigParser解析ini配置文件实例
- Python实现读写INI配置文件的方法示例
- Python实现加载及解析properties配置文件的方法
- Python使用ConfigParser模块操作配置文件的方法
相关文章推荐
- python解析模块(ConfigParser)使用方法
- Python解析命令行读取参数--argparse模块使用方法
- 详解Python使用simplejson模块解析JSON的方法
- 详解Python使用simplejson模块解析JSON的方法
- Python使用内置json模块解析json格式数据的方法
- ##########(python 解析参数方法 可用) Python optionParser模块的使用方法 #######
- Python 解析配置文件 (ConfigParser)模块
- Python模块安装: pip常见的使用方法
- 详解Python中使用base64模块来处理base64编码的方法
- Python配置文件解析模块ConfigParser使用实例
- python使用xlrd模块读写Excel文件的方法
- 详解使用python的logging模块在stdout输出的两种方法
- 零基础自学用Python 3开发网络爬虫(五): 使用第三方模块快速抓取与解析
- Python random模块(获取随机数)常用方法和使用例子
- Python_使用csv模块解析csv文件
- Python中time,datetime模块的常见使用方法
- (转)python标准日志模块logging的使用方法
- Python optionParser模块的使用方法
- 使用Python解析JSON数据的基本方法
- python使用wmi模块获取windows下硬盘信息的方法