[Pythonmac-SIG] How to print unicode to OS-X Terminal.app
Robin Dunn
robin at alldunn.com
Thu Feb 14 22:44:26 CET 2008
Christopher Barker wrote:
> Kent Johnson wrote:
>> 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
>
> Thanks for the links.
>
> I'm nervous about messing with sys.setdefaultencoding() too - partly
> because I have no idea what all the implications are.
>
> It sure would be nice to be able to just change it for "print" though.
> Even if it's going to us ascii, it really should use "replace" or
> "ignore" -- it's much better to get something, rather than an error.
> It's just a handy utility function after all.
>
> Maybe I can re-map print to something like:
>
> TerminalEncoding = "utf-8"
> def uni_print(object):
> sys.stdout.write(unicode(object).encode(TerminalEncoding))
> sys.stdout.write("\n")
>
>
> but print is a statement, rather than a function, so I don't know how to
> do that. I may start using a utility function like that for my code, though.
Just replace sys.stdout with an object with a write() method that does
what you want. If you need to use the original stdout for the actual
output then you can get to is from sys.__stdout__.
--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!
More information about the Pythonmac-SIG
mailing list