print and unicode strings

Jason Orendorff jason at jorendorff.com
Tue Feb 19 22:06:23 EST 2002


The following 3 lines are now in my PYTHONSTARTUP file
on Windows.

# Enable friendly Unicode output
import codecs
sys.stdout = codecs.getwriter('cp437')(sys.stdout, 'replace')

Check it out.

  >>> print u'\xbfQu\xe9 pas\xf3?'
  ¿Qué pasó?

What, no UnicodeError?  What happened?

It's even better in IDLE, with 'utf-8' as the encoding.
Try it.  This is guaranteed to make you grin.

  # in IDLE
  >>> sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
  >>> print u'\N{COMET}'

I think IDLE should do this by default.  It's fine for
Python to "refuse to guess" encodings, but in the case of Tk
it's UTF-8 on all platforms... right?

## Jason Orendorff    http://www.jorendorff.com/





More information about the Python-list mailing list