Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #75045

Re: Unicode, stdout, and stderr

From Akira Li <4kir4.1i@gmail.com>
Subject Re: Unicode, stdout, and stderr
Date 2014-07-23 05:01 +0400
References <mailman.12161.1406009902.18130.python-list@python.org> <53ce0b96$0$29897$c3e8da3$5496439d@news.astraweb.com> <lql32r$d5$1@ger.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.12209.1406077282.18130.python-list@python.org> (permalink)

Show all headers | View raw


"Frank Millman" <frank@chagford.com> writes:

> "Steven D'Aprano" <steve@pearwood.info> wrote in message 
> news:53ce0b96$0$29897$c3e8da3$5496439d@news.astraweb.com...
>> On Tue, 22 Jul 2014 08:18:08 +0200, Frank Millman wrote:
>>
>>> This is not important, but I would appreciate it if someone could
>>> explain the following, run from cmd.exe on Windows Server 2003 -
>>>
>>> C:\>python
>>> Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32
>>> bit (In
>>> tel)] on win32
>>> Type "help", "copyright", "credits" or "license" for more information.
>>>>>> x = '\u2119'
>>>>>> x  # this uses stderr
>>> '\u2119'
>>

>>> It seems that there is a difference between writing to stdout and
>>> writing to stderr.
>>
>> I would be surprised if that were the case, but I don't have a Windows
>> box to test it. Try this:
>>
>>
>> import sys
>> print(x, file=sys.stderr)  # I expect this will fail
>
> It does not fail.
>> print(repr(x), file=sys.stdout)  # I expect this will succeed
>
> It fails.

Check sys.stderr.errors attribute. Try

    >>> import sys
    >>> x = '\u2119'
    >>> x.encode(sys.stderr.encoding, sys.stderr.errors) # succeed
    >>> x.encode(sys.stdout.encoding, sys.stdout.errors) # fail

sys.stderr uses 'backslashreplace' error handler that is why you see
\u2119 instead of ℙ.

On Linux with utf-8 locale:

  >>> print('\u2119')
  ℙ
  >>> print(repr('\u2119'))
  'ℙ'
  >>> print(ascii('\u2119'))
  '\u2119'
  >>> '\u2119'
  'ℙ'
  >>> repr('\u2119')
  "'ℙ'"
  >>> ascii('\u2119')
  "'\\u2119'"

On Windows, try https://pypi.python.org/pypi/win_unicode_console

  C:\> pip install win-unicode-console
  C:\> py -i -m run

It is alpha but your feedback may improve it
https://github.com/Drekin/win-unicode-console/issues 

If you could also use a GUI console e.g.:

  C:\> py -3 -m idlelib

Or http://ipython.org/notebook.html

There are many other IDEs for Python e.g.,

http://stackoverflow.com/q/81584/what-ide-to-use-for-python


--
Akira

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Unicode, stdout, and stderr "Frank Millman" <frank@chagford.com> - 2014-07-22 08:18 +0200
  Re: Unicode, stdout, and stderr wxjmfauth@gmail.com - 2014-07-21 23:54 -0700
  Re: Unicode, stdout, and stderr Steven D'Aprano <steve@pearwood.info> - 2014-07-22 06:58 +0000
    Re: Unicode, stdout, and stderr "Frank Millman" <frank@chagford.com> - 2014-07-22 09:15 +0200
    Re: Unicode, stdout, and stderr Lele Gaifax <lele@metapensiero.it> - 2014-07-22 09:36 +0200
    Re: Unicode, stdout, and stderr Akira Li <4kir4.1i@gmail.com> - 2014-07-23 05:01 +0400
      Re: Unicode, stdout, and stderr wxjmfauth@gmail.com - 2014-07-23 00:35 -0700
  Re: Unicode, stdout, and stderr wxjmfauth@gmail.com - 2014-07-22 00:07 -0700

csiph-web