python - How to preserve keys with capital letters read from a config-file with configparser? -


this question has answer here:

when reading out config file python's configparser package, key names lowercase strings. know how read strings preserving capital , uppercase words?

for example:

$cat config.cfg  [default] key_1 = someword key_2 = word  $ python3 >>> configparser import configparser >>> cf = configparser() >>> cf.read('./config.cfg') ['./config.cfg'] >>> print(cf.defaults()) ordereddict([('key_1', 'someword'), ('key_2', 'another word')]) 

thanks help!

yes, keys automatically transformed lowercase during read/write operations. mentioned in last sentence of the quick start section of configparser docs.

to not have effect can set parsers' optionxform (a callable) return option rather transform lowercase:

>>> configparser import configparser >>> c = configparser() >>> c.optionxform = lambda option: option >>> c.read('./config.cfg') ['./config.cfg'] 

now keys preserved defined:

>>> c.defaults() ordereddict([('key_1', 'someword'), ('key_2', 'another word')]) 

of course can customize liking, if, example, wanted keys uppercase set in optionxform:

>>> cf = configparser() >>> cf.optionxform = lambda option: option.upper() >>> cf.read('./config.cfg') ['./config.cfg'] >>> cf.defaults() ordereddict([('key_1', 'someword'), ('key_2', 'another word')]) 

Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -