2

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)

ini file before edit:

[GENERAL]
OptionEntry1 = 10
OptionEntry2 = 20
OptionEntry3 = 30
OptionEntry4 = 40
OptionEntry5 = 50

ini file after edit:

[GENERAL]
optionentry1 = 10
optionentry2 = 20
optionentry3 = 30
optionentry4 = 100
optionentry5 = 50

2 Answers2

3

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.

Pierre
  • 166
  • 1
  • 1
  • 7
3
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

rsudip90
  • 799
  • 1
  • 7
  • 24