白天的小萤火虫

导航

python库的学习系列之 13.2. ConfigParser — Configuration file parser

13.2. ConfigParser — Configuration file parser

提供了一个ConfigParser.RawConfigParser方法,处理配置文件很好用,我们可以将配置文件读到一个map里面,方便程序访问。见例子:

如果有以下一个配置conf文件,定义了两个section

[global]
onlinee
=./mypath/ 
status_success
=myvalue

[online]

query_word
=myname

我们可以通过下面的程序去方便的读取他们:

import
负责读取global section的值:
def global_init(self):
global_map
= {}
onlinee
= self.config_parser.get("global","onlinee")
if onlinepecode == "":
self.logger.error(
"[%s] get onlinee path None!!!" %self.__class__)
return None
else:
global_map.update(onlinee
= onlinee)

负责读取online section的值
def get_parameter(self,cf_parameter):
cf_parameter_map = {}
online = self.config_parser.get(cf_parameter,"
online")
if online == "":
online = None
self.logger.error("[%s][get %s online is None!!!]" %(self.__class__, cf_parameter))
cf_parameter_map.update(online = online)
else:
cf_parameter_map.update(
online = online))
可以,使用非常简单。

posted on 2011-01-22 15:07  白天的小萤火虫  阅读(242)  评论(0编辑  收藏  举报