Configparser With Unicode Items
Solution 1:
The ConfigParser.readfp()
method can take a file object, have you tried opening the file object with the correct encoding using the codecs module before sending it to ConfigParser like below:
cfg.readfp(codecs.open("myconfig", "r", "utf8"))
For Python 3.2 or above, readfp()
is deprecated. Use read_file()
instead.
Solution 2:
In python 3.2 encoding
parameter was introduced to read()
, so it can now be used as:
cfg.read("myconfig", encoding='utf-8')
Solution 3:
Try to overwrite the write
function in RawConfigParser()
like this:
classConfigWithCoder(RawConfigParser):
defwrite(self, fp):
"""Write an .ini-format representation of the configuration state."""if self._defaults:
fp.write("[%s]\n" % "DEFAULT")
for (key, value) in self._defaults.items():
fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
fp.write("\n")
for section in self._sections:
fp.write("[%s]\n" % section)
for (key, value) in self._sections[section].items():
if key == "__name__":
continueif (value isnotNone) or (self._optcre == self.OPTCRE):
iftype(value) == unicode:
value = ''.join(value).encode('utf-8')
else:
value = str(value)
value = value.replace('\n', '\n\t')
key = " = ".join((key, value))
fp.write("%s\n" % (key))
fp.write("\n")
Solution 4:
Seems to be a problem with the ConfigParser version for python 2x, and version for 3x is free of this problem. In this issue of the Python Bug Tracker, the status is Closed + WONTFIX.
I've fixed it editing the ConfigParser.py file. In the write method (about the line 412), change:
key = " = ".join((key, str(value).replace('\n', '\n\t')))
by
key = " = ".join((key, str(value).decode('utf-8').replace('\n', '\n\t')))
I don't know if it's a real solution, but tested in Windows 7 and Ubuntu 15.04, works like a charm, and I can share and work with the same .ini file in both systems.
Solution 5:
what I did is just:
file_name = file_name.decode("utf-8")
cfg.read(file_name)
Post a Comment for "Configparser With Unicode Items"