python3 configparser 区分大小写

configparser.optionxform(option)
这个方法会转换每次 read, get, 或 set 操作的选项名称。 默认会将名称转换为小写形式。 这也意味着当一个配置文件被写入时,所有键都将为小写形式。

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

python2的重载处理方法如下:

class MyConfigParser(ConfigParser.ConfigParser):
    """
    set ConfigParser options for case sensitive.
    """
    def __init__(self, defaults=None):
        ConfigParser.ConfigParser.__init__(self, defaults=defaults)
 
    def optionxform(self, optionstr):
        return optionstr

python3的重载处理方法如下:

config= configparser.RawConfigParser()
config.optionxform = lambda option: option

官方文档:
https://docs.python.org/3.6/library/configparser.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。