singing the praises of unicode and codecs

Steven Bethard steven.bethard at gmail.com
Fri Dec 10 13:43:17 EST 2004


I just wanted to thank Python for making encodings so easy!

I recently discovered that one of the tools I use stores everything in 
UTF-8, and so I was getting some off-by-one errors because I was 
treating things as strings.  I added

     def __unicode__(self):
         return str(self).decode('utf-8')

to the base object in the hierarchy, and wrapped my popen calls with 
readers and writers:

     file_in, file_out, file_err = _os.popen3(self.command)
     file_in = codecs.getwriter(self.encoding)(file_in)
     file_out = codecs.getreader(self.encoding)(file_out)
     file_err = codecs.getreader(self.encoding)(file_err)

and voilà! Everything works perfect!

Thank you Python!

Steve



More information about the Python-list mailing list