Yet another unicode WTF

Ben Finney ben+python at
Fri Jun 5 05:51:03 EDT 2009

Paul Boddie <paul at> writes:

> The only way to think about this (in Python 2.x, at least) is to
> consider stream and file objects as things which only understand plain
> byte strings. Consequently, use of the codecs module is required if
> receiving/sending Unicode objects from/to streams and files.

Actually strings in Python 2.4 or later have the ‘encode’ method, with
no need for importing extra modules:

$ python -c 'import sys; sys.stdout.write(u"\u03bb\n".encode("utf-8"))'

$ python -c 'import sys; sys.stdout.write(u"\u03bb\n".encode("utf-8"))' > foo ; cat foo

 \      “Life does not cease to be funny when people die any more than |
  `\  it ceases to be serious when people laugh.” —George Bernard Shaw |
_o__)                                                                  |
Ben Finney

More information about the Python-list mailing list