python解析模块(ConfigParser)使用方法
2013-12-10 00:00
1186 查看
测试配置文件test.conf内容如下:
测试配置文件中有两个区域,first和second,另外故意添加一些空格、换行。
下面解析:
获得每个区域的属性值:
输出:
好了,以上就是基本的使用过程,下面是动态的写入配置,
上面是向test2.ini写入配置信息。
[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使用内置json模块解析json格式数据的方法
- ##########(python 解析参数方法 可用) Python optionParser模块的使用方法 #######
- Python解析命令行读取参数--argparse模块使用方法
- 详解Python使用simplejson模块解析JSON的方法
- 详解Python使用simplejson模块解析JSON的方法
- Android中XML解析模块的使用方法
- python使用电子邮件模块smtplib的方法
- Python 安装模块时,使用pip install xxx安装时,很慢的解决方法
- python使用xlrd模块读写Excel文件的方法
- Python中使用ConfigParser解析ini配置文件实例
- 详解Python中使用base64模块来处理base64编码的方法
- Python使用logging模块实现打印log到指定文件的方法
- Python使用pymongo模块操作MongoDB的方法示例
- Python命令行参数解析模块optparse使用实例
- 使用Python解析JSON数据的基本方法
- PYTHON 模块中使用了traceback 模块报错时的解决方法
- 【转】使用Python中HTTPParser模块进行简单的html解析
- python argparse模块解析命令行选项简单使用
- python标准日志模块logging的使用方法