python config module case not distinguish

内容纲要

Question

python configparser模块不区分大小写问题

Analysis

经源码分析,configparser内部存在自动转化小写代码.

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

Solution

找到原因,只需要重写该函数即可。

class myconf(configparser.ConfigParser):
    def __init__(self,defaults=None):
        configparser.ConfigParser.__init__(self,defaults=None)
    # 区分大小写
    def optionxform(self, optionstr):
        return optionstr

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注