首页 > 编程语言 > 详细

Python-ConfigParser获取配置项名称大小写问题

时间:2017-05-15 12:49:57      阅读:1117      评论:0      收藏:0      [点我收藏+]

C:\Python27\Lib\ConfigParser.py:

def optionxform(self, optionstr):
return optionstr.lower()

会将配置文件中的选项名改为小写

为了保持配置项名称的大小写格式,重写:

class myconf(ConfigParser.ConfigParser):
def __init__(self, defaults=None):
ConfigParser.ConfigParser.__init__(self, defaults=defaults)

# 这里重写了optionxform方法,直接返回选项名
def optionxform(self, optionstr):
return
optionstr

使用:
# 获取test.cfg
def get_config(self, cfg_file):
cfg_dict = {}
config = myconf()
config.read(cfg_file)

for sec in config.sections():
cfg_dict[sec] = {}
for op in config.items(sec):
cfg_dict[sec][op[0]] = op[1]
return cfg_dict
 

Python-ConfigParser获取配置项名称大小写问题

原文:http://www.cnblogs.com/workingdiary/p/6855726.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!