[Pythonmac-SIG] How to print unicode to OS-X Terminal.app

Christopher Barker Chris.Barker at noaa.gov
Thu Feb 14 01:27:53 CET 2008

skip at pobox.com wrote:
>     Chris> So how to I get Python to convert to utf-8 with a print
>     Chris> statement, instead of ascii?
> The print statement can't do it directly, but you can encode Unicode objects
> using different charsets then print the result.  Try this:

>     >>> print unicode("\xef", "latin-1").encode("utf-8")
>     ï

Thanks skip, that works. Do what I'm doing is encoding the unicode 
object into a string with the utf-8 encoding. I'm surprised that that 
prints! I guess you can print any string, but I figured it would escape 
all the non-ascii values, and send that to the terminal.

The question is, will this work on other terminals?? And here is the 
answer (from http://www.jorendorff.com/articles/unicode/python.html):

To print data reliably, you must know the encoding that this display 
program expects.


The Windows console still emulates CP437. So this print statement will 
work, on Windows, under a console window.

   # Windows console mode only
   >>> s = u'\N{POUND SIGN}'
   >>> print s.encode('cp-437')

Several SSH clients display data using the Latin-1 character set; 
Tkinter assumes UTF-8, when 8-bit strings are passed into it. So in 
general it is not possible to determine what encoding to use with print.


I suppose what I would like is if I could change the default encoding 
that str() uses -- or at least change it to "replace" or "ignore" mode.

Boy, I'm looking forward to  all-unicode, all the time.


Christopher Barker, Ph.D.

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov

More information about the Pythonmac-SIG mailing list