singing the praises of unicode and codecs

S

Steven Bethard

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top