5 ms·
Excellent article. One of the clearest explanation of how to use unicode in python that I have seen yet.
by newt0311 18y ago
Excellent article. One of the clearest explanation of how to use unicode in python that I have seen yet.
- pmorici 18y agoYeah, but it leaves out one important bit that I've never been able to figure out. How do you use print to output a UTF-8 or some other Unicode encoding w/o using the repr() call?
- tsuraan 18y agoSet the environmental variable LC_CTYPE to something like "en_US.utf-8" for utf-8 output. I don't know where it's documented; I just found it on some guy's blog a while ago. This also apparently doesn't work if python's stdin isn't a console (i.e. it's a pipe), but for interactive sessions it's good enough.
- inklesspen 18y agoSimply encode your unicode string in utf-8 and print the resulting bytestring. Simple, huh? Okay, say you don't want to do u"somestring".encode('utf-8') all the time. Python has a set of classes that will wrap files and transparently encode and decode for you. And stdout is just another file. import sys import codecs sys.stdout = codecs.getwriter("utf-8")(sys.stdout) line = u"\u0411" print line