Preserve case in ConfigParser?

前端 未结 5 1850
闹比i
闹比i 2020-12-04 23:00

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 pa

5条回答
  •  清歌不尽
    2020-12-04 23:30

    I know this question is answered, but I thought some people might find this solution useful. This is a class that can easily replace the existing ConfigParser class.

    Edited to incorporate @OozeMeister's suggestion:

    class CaseConfigParser(ConfigParser):
        def optionxform(self, optionstr):
            return optionstr
    

    Usage is the same as normal ConfigParser.

    parser = CaseConfigParser()
    parser.read(something)
    

    This is so you avoid having to set optionxform every time you make a new ConfigParser, which is kind of tedious.

提交回复
热议问题