configparser使用

sudo easy_install dict4ini

ConfigParser模块是python自带的读取配置文件的模块.通过他可以方便的读取配置文件. 这篇文章简单介绍一下读取配置文件的方法.

配置文件.顾名思议就是存放配置的文件.下面是个例子
[info]
age = 21
name = chen
sex = male

其中[ ] 中的info是这段配置的名字
下面age,name都是属性

下面的代码演示了如何读取配置文件.和修改配置中变量的值

from __future__ import with_statement
import ConfigParser
config
=ConfigParser.ConfigParser()
with open('testcfg.cfg','rw') as cfgfile:
    config.readfp(cfgfile)
    name
=config.get('info','name')
    age
=config.get('info','age')
    
print name
    
print age
    config.set(
'info','sex','male')
    config.set(
'info','age','21')
    age
=config.get('info','age')
    
print name
    
print age

原文地址:https://www.cnblogs.com/lexus/p/1833710.html