python 提供INI配置文件的操作 ConfigParser

原文地址:http://www.cnblogs.com/pumaboyd/archive/2008/08/11/1265416.html

红色的为标注信息

+++++++++++++++++引用+++++++++++++++++++++

>PY提供INI配置文件的操作


关于配置文件,很直观的感觉就是XML文件。对于XML文件的使用大家还是很喜欢的。但有时候只是简单的一个程序,实现一个简单的name:value关系。用XML文件就没这个必要。这种要求很符合MS的INI文件格式。所以这里主要介绍一下对INI文件的操作方式,而且最近写的第一个PY应用程序也是使用了INI
什么是INI  文件
PY所支持的INI文件还是和Windows系统所定义有不同,它不但支持name=value的形式,还支持name:value的形式

>PY对INI配置文件读取提供的类支持


PY的ConfigParser Module中定义了3个类对INI文件进行操作。分别是RawConfigParser、ConfigParser、SafeConfigParser
RawCnfigParser是最基础的INI文件读取类
ConfigParser、SafeConfigParser支持对$(value)s变量的支持。

>RawConfigParser类的使用方法 (建议使用)


int文件

[weburl]
urlname
=http://pumaboyd.cnblogs.com


Test.py文件

import ConfigParser, os
from __future__ import with_statement
cfg 
= ConfigParser.RawConfigParser()
with open(
"app.ini") as fobj
    cfg.readfp( fobj)
print cfg.get("weburl","urlname"

 可以查看一下help(cfg.readfp)

readfp是读取一个 file object 类似于 something.readlines() 的对象。

使用with用法,将会节省很多内存,这里比较赞同。

>ConfigParser类的使用方法


Configration类是从RawConfigParser扩展过来的,可以支持$()s变量。
对RawConfigParserd的get(),items()进行了扩展

 

int文件

[DEFAULT]
val
=pumaboyd
[weburl]
name
=%(val)s 

 

Test.py文件

复制代码
import ConfigParser, os
from __future__ import with_statement
cfg 
= ConfigParser.ConfigParser()
with open(
"app.ini") as fobj
    cfg.readfp( fobj)
print cfg.defaults()
print cfg.get("weburl","name"
复制代码

 

可以看到cfg.get("weburl","name") 输入的pumaboyd。如果这里采用的是RawConfigParser,你将看到输出的是%(val)s。
这里需要注意的一个地方就是DEFAULT这个默认节点。只能通过cfg.defaults()读取到。cfg.sections()是不包含DEFAULT这个节点的。

>SafeConfigParser类的使用方法


是从ConfigParser继承过来,其实是对RawConfigParser进行了扩展,可以支持$()s变量

int文件

[DEFAULT]
val
=pumaboyd
[weburl]
name
=abcd 

 

Test.py文件

复制代码
import ConfigParser, os
from __future__ import with_statement
cfg 
= ConfigParser.SaftConfigParser()
with open(
"app.ini") as fobj
    cfg.readfp( fobj)
cfg.set(
"weburl","name","&(val)s")
print cfg.get("weburl","name")
复制代码


你将看到输入结果是pumaboyd。如果采用的RawConfigParser,你就看到输出的是%()s

>如何修改INI文件


RawConfigParser、SafeConfigParser、ConfigParser中的SET、Remove等方法都只是对ConfigParser对象的修改,并没有真正的保存到INI文件中。所以,需要通过Write方法(3个类中都有这个方法),将修改写回INI文件中。

ini文件

 

[weburl]
name
=abcd 

 

Test.py文件

复制代码
import ConfigParser, os
from __future__ import with_statement
cfg 
= ConfigParser.ConfigParser()
with open(
"app.ini") as fobj
    cfg.readfp( fobj)
cfg.set(
"weburl","name","pumaboyd"

with open(
"app.ini","w") as fwobj
    cfg.write(fwobj) 
复制代码

--=阅读快乐=--

欢迎访问我的新鱼塘 www.pumaboyd.com

++++++++++++++++引用结束++++++++++++++++++++

另外的一个引用。这个引用比较初级。结合tornado 的 options 来看,简单的ConfigParser 并没有将配置文件中的内容对象话,所以还需要进一步的续写。

在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是ConfigParser,这里简单的做一些介绍。
    ConfigParser解析的配置文件的格式比较象ini的配置文件格式,就是文件中由多个section构成,每个section下又有多个配置项,比如: 

[db]
db_host=127.0.0.1
db_port=3306
db_user=root
db_pass=password
[concurrent]
thread=10
processor=20

    假设上面的配置文件的名字为test.conf。里面包含两个section,一个是db, 另一个是concurrent, db里面还包含有4项,concurrent里面有两项。这里来做做解析:
复制代码
#-*- encoding: gb2312 -*-
import ConfigParser
import
string, os, sys

cf
=
ConfigParser.ConfigParser()
cf.read(
"test.conf"
)
# 返回所有的section

s = cf.sections()
print'section:'
, s

o
= cf.options("db"
)
print'options:'
, o

v
= cf.items("db"
)
print'db:'
, v

print'-'*60

#可以按照类型读取出来
db_host = cf.get("db", "db_host")
db_port
= cf.getint("db", "db_port"
)
db_user
= cf.get("db", "db_user"
)
db_pass
= cf.get("db", "db_pass"
)

# 返回的是整型的

threads = cf.getint("concurrent", "thread")
processors
= cf.getint("concurrent", "processor"
)

print"db_host:"
, db_host
print"db_port:"
, db_port
print"db_user:"
, db_user
print"db_pass:"
, db_pass

print"thread:"
, threads
print"processor:"
, processors
#修改一个值,再写回去

cf.set("db", "db_pass", "zhaowei")
cf.write(open(
"test.conf", "w"))
复制代码
posted @ 2014-03-03 09:15  spaceship9  阅读(494)  评论(0编辑  收藏  举报