在ConfigParser中保留大小写? [英] Preserve case in ConfigParser?

查看:264
本文介绍了在ConfigParser中保留大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Python的 ConfigParser 模块保存设置。对于我的应用程序,请务必在各部分中保留每个名称的大小写。该文档提到将str()传递给 ConfigParser.optionxform()可以完成此操作,但对我不起作用。名称均为小写。我错过了什么吗?

I have tried to use Python's ConfigParser module to save settings. For my app it's important that I preserve the case of each name in my sections. The docs mention that passing str() to ConfigParser.optionxform() would accomplish this, but it doesn't work for me. The names are all lowercase. Am I missing something?

<~/.myrc contents>
[rules]
Monkey = foo
Ferret = baz

我得到的Python伪代码:

Python pseudocode of what I get:

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')]


推荐答案

文档令人困惑。它们是什么意思?

The documentation is confusing. What they mean is this:

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,而不是调用它;重写可以在子类或实例中完成。覆盖时,将其设置为函数(而不是调用函数的结果)。

I.e. override optionxform, instead of calling it; overriding can be done in a subclass or in the instance. When overriding, set it to a function (rather than the result of calling a function).

我现在报告了这是一个错误,此问题已得到修复。

I have now reported this as a bug, and it has since been fixed.

这篇关于在ConfigParser中保留大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆