我尝试使用Python的ConfigParser模块来保存设置。对于我的应用程序来说,在我的部分中保留每个名称的大小写是很重要的。文档中提到,将str()传递给ConfigParser.optionxform()可以实现这一点,但它不适用于我。名字都是小写的。我是不是遗漏了什么?
<~/.myrc contents>
[rules]
Monkey = foo
Ferret = baz
我得到的Python伪代码:
import ConfigParser,os
def get_config():
config = ConfigParser.ConfigParser()
config.optionxform(str())
try:
config.read(os.path.expanduser('~/.myrc'))
return config
except Exception, e:
log.error(e)
c = get_config()
print c.options('rules')
[('monkey', 'foo'), ('ferret', 'baz')]
发布于 2009-10-23 15:33:21
文档令人困惑。他们的意思是:
import ConfigParser, os
def get_config():
config = ConfigParser.ConfigParser()
config.optionxform=str
try:
config.read(os.path.expanduser('~/.myrc'))
return config
except Exception, e:
log.error(e)
c = get_config()
print c.options('rules')
例如,覆盖optionxform,而不是调用它;覆盖可以在子类或实例中完成。重写时,将其设置为函数(而不是调用函数的结果)。
我现在已经报告了this as a bug,它已经被修复了。
发布于 2014-05-24 03:02:39
因为我在创建对象后立即设置了optionxform
config = ConfigParser.RawConfigParser()
config.optionxform = str
发布于 2018-08-19 12:09:10
添加到您的代码中:
config.optionxform = lambda option: option # preserve case for letters
https://stackoverflow.com/questions/1611799
复制相似问题