Yet another unicode WTF

Ben Finney ben+python at benfinney.id.au
Fri Jun 5 05:51:03 EDT 2009


Paul Boddie <paul at boddie.org.uk> 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