简体   繁体   中英

Editing ini file using python “ConfigParser” will replace all ini entries to lower keys

The code snippet below can edit an ini file, but will replace all ini entries to lower case:

config = ConfigParser.RawConfigParser()
config.read("test.ini")
config.set("GENERAL", "OptionEntry4", "100")
with open("test.ini", 'w') as configfile:
    config.write(configfile)


OptionEntry1 = 10
OptionEntry2 = 20
OptionEntry3 = 30
OptionEntry4 = 40
OptionEntry5 = 50


optionentry1 = 10
optionentry2 = 20
optionentry3 = 30
optionentry4 = 100
optionentry5 = 50

according to the documentation : "All option names are passed through the optionxform() method. Its default implementation converts option names to lower case."

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

should fix it.

config = ConfigParser.RawConfigParser()
config.optionxform = str
config.read("test.ini")
config.set("GENERAL", "OptionEntry4", "100")
with open("test.ini", 'w') as configfile:
    config.write(configfile)

Read the doc: https://docs.python.org/2/library/configparser.html#ConfigParser.RawConfigParser.optionxform

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM