[Pythonmac-SIG] How to print unicode to OS-X Terminal.app
Hi all, I generally use Terminal.app as my terminal with python. With all the default settings, it appears to be a showing up as an ascii terminal to python. This means that if I do: print UnicodeObject it tried to encode it as ascii, which often fails. However, it seems that OS-X is pretty unicode savy, so you'd think I should be able to do this. Indeed, if I go to setting, I see that under display, it's set to UTF-8. So how to I get Python to convert to utf-8 with a print statement, instead of ascii? thanks, -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/ORR(206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception [EMAIL PROTECTED] ___ Pythonmac-SIG maillist - Pythonmac-SIG@python.org http://mail.python.org/mailman/listinfo/pythonmac-sig
Re: [Pythonmac-SIG] How to print unicode to OS-X Terminal.app
[EMAIL PROTECTED] 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. Thanks, -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/ORR(206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception [EMAIL PROTECTED] ___ Pythonmac-SIG maillist - Pythonmac-SIG@python.org http://mail.python.org/mailman/listinfo/pythonmac-sig
Re: [Pythonmac-SIG] How to print unicode to OS-X Terminal.app
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: unicode(\xef, latin-1) u'\xef' unicode(\xef, latin-1).encode(utf-8) '\xc3\xaf' print unicode(\xef, latin-1).encode(utf-8) ï -- Skip Montanaro - [EMAIL PROTECTED] - http://www.webfast.com/~skip/ ___ Pythonmac-SIG maillist - Pythonmac-SIG@python.org http://mail.python.org/mailman/listinfo/pythonmac-sig
Re: [Pythonmac-SIG] How to print unicode to OS-X Terminal.app
Christopher Barker wrote: 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. You can, with sys.setdefaultencoding(). See here for discussion of how: http://blog.ianbicking.org/illusive-setdefaultencoding.html and here for arguments that this is a bad idea (mostly because it makes your code non-portable): http://faassen.n--tree.net/blog/view/weblog/2005/08/02/0 Kent ___ Pythonmac-SIG maillist - Pythonmac-SIG@python.org http://mail.python.org/mailman/listinfo/pythonmac-sig