ConfigParser and unicode: a simple solution?

Frank Niessink frank at niessink.com
Wed Feb 22 17:42:37 EST 2006


Dear list members,

I'm using ConfigParser to read and write simple configuration files. One 
of the items written is the file the user is currently working on, so 
that the application can load that same file when the user starts the 
application for a new session.

However, filenames may not be simple ascii strings, but can very well be 
  unicode strings. Would a simple solution to store the filename in the 
.ini file be to encode it with 'unicode_escape' before calling 
configparse.set(section, setting, filename)? For example:

settings = SafeConfigParser(...)
encoded_filename = filename.encode('unicode_escape')
settings.set('file', 'currentfilename', encoded_filename)

And of course doing the reverse when getting the filename:

settings = SafeConfigParser(...)
encoded_filename = settings.get('file', 'currentfilename')
filename = filename.decode('unicode_escape')

Would this work? Any caveats?

Thanks, Frank

PS: I'm aware of a bunch of alternatives to ConfigParsers, so if the 
above solution is not feasible, I'll probably have to incorporate one of 
these alternatives in my app.



More information about the Python-list mailing list