95

I found one interesting observation. I had written one config file read program as,

import ConfigParser
class  ConfReader(object):
    ConfMap = dict()

    def __init__(self):
        self.config = ConfigParser.ConfigParser()
        self.config.read('./Config.ini')
        self.__loadConfigMap()

    def __loadConfigMap(self):
        for sec in self.config.sections():
            for key,value in self.config.items(sec):
                print 'key = ', key, 'Value = ', value
                keyDict = str(sec) + '_' + str(key)
                print 'keyDict = ' + keyDict  
                self.ConfMap[keyDict] = value

    def getValue(self, key):
        value = ''
        try:
            print ' Key = ', key
            value = self.ConfMap[key] 
        except KeyError as KE:
            print 'Key', KE , ' didn\'t found in configuration.'
    return value

class MyConfReader(object):
    objConfReader = ConfReader()

def main():
     print MyConfReader().objConfReader.getValue('DB2.poolsize')
     print MyConfReader().objConfReader.getValue('DB_NAME')

if __name__=='__main__':
    main()

And my Config.ini file looks like,

[DB]
HOST_NAME=localhost
NAME=temp
USER_NAME=postgres
PASSWORD=mandy

The __loadConfigMap() works just fine. But while reading the key and values, it is making the keys lower case. I didn't understand the reason. Can any one please explain why it is so?

Mandy
  • 1,087
  • 1
  • 10
  • 14
  • 1
    Possible duplicate of [Preserve case in ConfigParser?](https://stackoverflow.com/questions/1611799/preserve-case-in-configparser) – funky-future Oct 05 '17 at 15:03

1 Answers1

165

ConfigParser.ConfigParser() is documented to behave this way, in the Mapping Protocol Access section:

By default, all keys in sections are accessible in a case-insensitive manner. E.g. for option in parser["section"] yields only optionxform’ed option key names. This means lowercased keys by default.

That's because this module parses Windows INI files which are expected to be parsed case-insensitively.

You can disable this behaviour by replacing the ConfigParser.optionxform() function:

self.config = ConfigParser.ConfigParser()
self.config.optionxform = str

str passes through the options unchanged.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • @Martijin this means I need to replace the code of ConfigParser.RawConfigParser(). Fine, thank you for the explanation. Works for me. – Mandy Oct 14 '13 at 12:01
  • 11
    Thanks for explaining **why** `ConfigParser` is converting the keys to lowercase. – abaumg Jan 12 '16 at 15:22
  • With this option, i see that CAPITALS are retained for the keys. In my ini file i don't have any spaces between the key and value. But, config parser is adding a space before and after the equal to. For example: if i have KEY=value in my ini file, it is changed to KEY = value. Is there any option to avoid these extra spaces? – sridhar249 Sep 29 '16 at 16:42
  • I found the solution for the space issue in here: http://stackoverflow.com/questions/14021135/how-to-remove-spaces-while-writing-in-ini-file-python – sridhar249 Sep 29 '16 at 16:57
  • Mypy complains that we 'cannnot assign to a method' on this line: self.config.optionxform = str – uuu777 Sep 26 '22 at 02:04
  • @uuu777: well, Mypy is wrong there. This is one of those exceptions where it is okay to shadow the class implementation of a method. Just add `# type: ignore` on the line. – Martijn Pieters Oct 07 '22 at 11:10