ConfigParser and Unicode
Python has a very convenient module for saving and reading ini-like configuration files called ConfigParser .
When using it, I had a problem related to saving Unicode strings to a file. In some subtle cases (for example, it showed me when the application was running under Windows XP), when reading or writing such parameters, a string conversion error pops up.
I could not find ready-made solutions on the Internet, although there are quite a lot of questions about “how to make it always work” - they usually answer in the spirit of “ask the author of the module to fix it”.
I want to offer my solution for those who use Python 2.X - it is quite simple and helps to solve this problem.
First, you need to inherit the RawConfigParser class by overriding the write () method - namely, replacing all str () calls with unicode () calls :
Secondly, writing and reading the configuration file must be done with a wrapper for open () from the codecs module , which must be specified with utf-8 as the encoding. In the case of loading, this can be done if you use readfp () rather than read () for reading :
I hope someone comes in handy. If you have a more beautiful and successful solution, I will be glad to hear it.
When using it, I had a problem related to saving Unicode strings to a file. In some subtle cases (for example, it showed me when the application was running under Windows XP), when reading or writing such parameters, a string conversion error pops up.
I could not find ready-made solutions on the Internet, although there are quite a lot of questions about “how to make it always work” - they usually answer in the spirit of “ask the author of the module to fix it”.
I want to offer my solution for those who use Python 2.X - it is quite simple and helps to solve this problem.
First, you need to inherit the RawConfigParser class by overriding the write () method - namely, replacing all str () calls with unicode () calls :
Copy Source | Copy HTML- class UnicodeConfigParser(ConfigParser.RawConfigParser):
-
- def __init__(self, *args, **kwargs):
- ConfigParser.RawConfigParser.__init__(self, *args, **kwargs)
-
- def write(self, fp):
- """Fixed for Unicode output"""
- if self._defaults:
- fp.write("[%s]\n" % DEFAULTSECT)
- for (key, value) in self._defaults.items():
- fp.write("%s = %s\n" % (key, unicode(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__":
- fp.write("%s = %s\n" %
- (key, unicode(value).replace('\n','\n\t')))
- fp.write("\n")
-
- # This function is needed to override default lower-case conversion
- # of the parameter's names. They will be saved 'as is'.
- def optionxform(self, strOut):
- return strOut
-
Secondly, writing and reading the configuration file must be done with a wrapper for open () from the codecs module , which must be specified with utf-8 as the encoding. In the case of loading, this can be done if you use readfp () rather than read () for reading :
Copy Source | Copy HTML- import codecs
-
- # Saving
-
- confFile = codecs.open('myConfig.ini', 'w', 'utf-8')
- config = UnicodeConfigParser()
- # ...
- config.write(confFile)
- confFile.close()
-
- # Loading
-
- config = UnicodeConfigParser()
- config.readfp(codecs.open('myConfig.ini', "r", "utf-8"))
I hope someone comes in handy. If you have a more beautiful and successful solution, I will be glad to hear it.