unable to print Unicode characters in Python 3

Stephen Hansen apt.shansen at gmail.com
Tue Jan 27 08:29:55 EST 2009


>
> Note that on Python 2.6 it works:
>
> Python 2.6.1 (r261:67517, Dec  4 2008, 16:51:00) [MSC v.1500 32 bit
> (Intel)] on
> win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> print unicode('\u20ac')
> \u20ac
>

Note that in Python 2.6 it expressly *does not* work, by your own example.
Although I think the example is wrong and that you need to print u'\u20ac'
in 2.6 unless you are importing unicode_literals from __future__; otherwise
'\u20ac' is an ASCII string of 6 characters.

"Works" would result, I'd think, in a unicode character being actually
written to stdout: that is not happening. Instead a backslash pattern of
ASCII characters meant to represent a Unicode character that -can't- be
written to that stream are being written to stdout. The two aren't even kind
of the same: but this is hardly something Python has a lot of control
over... Windows software printing unicode to stdout was always a bit broken.
IF you set the code page you could print some -- and IF you changed the font
on the command prompt to a /non/-default font you could actually display
others -- but it really is sort of a sucky situation, Unicode in Console
windows on Windows.

I really don't know how Python could do it better here: between the default
code page on Windows being completely lame, the default font not supporting
Unicode really... ISTM that having cross-platform apps which output unicode
(really UTF8 probably) to stdout are just not really doable. But if you
really want to you can always rebind sys.stdout with an error mode of
"backslashreplace" or "xmlcharrefreplace" (which I do in one situation where
I have a subprocess that has to output 'unicode' to a parent process)
instead of 'strict'.

--Stephen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090127/4da947a8/attachment-0001.html>


More information about the Python-list mailing list