(A Possible Solution) Re: preferred way to set encoding for print

Mark Tolonen metolone+gmane at gmail.com
Thu Sep 17 06:50:03 CEST 2009


"~flow" <wolfgang.lipp at gmail.com> wrote in message 
news:643ca91c-b81c-483c-a8af-65c93b593e1f at r33g2000vbp.googlegroups.com...
>On Sep 16, 7:16 am, "Mark Tolonen" <metolone+gm... at gmail.com> wrote:
>> Setting PYTHONIOENCODING overrides the encoding used for 
>> stdin/stdout/stderr
>> (See the Python help for details), but if your terminal doesn't support 
>> the
>> encoding that won't help.
[snip]
>what has changed in python is that they now somehow find out about the
>terminal's encoding, and then put that encoding into place and defend
>it with teeth and claws. it is simply not easy to take control of that
>setting.

A couple more tips, PYTHONIOENCODING takes an optional errorhandler:

C:\>set PYTHONIOENCODING=cp437:xmlcharrefreplace
C:\>python
Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit (Intel)] 
on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print('Hello \u5000\u5001')
Hello &#20480;&#20481;

You can also write directly to stdout with byte strings (Note: my terminal 
doesn't support UTF-8, but no error):

>>> import sys
>>> sys.stdout.buffer.write('\u5000'.encode('utf8'))
sÇÇ3

-Mark





More information about the Python-list mailing list