Unicode, stdout, and stderr
4kir4.1i at gmail.com
Wed Jul 23 03:01:08 CEST 2014
"Frank Millman" <frank at chagford.com> writes:
> "Steven D'Aprano" <steve at pearwood.info> wrote in message
> news:53ce0b96$0$29897$c3e8da3$5496439d at 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 -
>>> 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
>>> 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:
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
If you could also use a GUI console e.g.:
C:\> py -3 -m idlelib
There are many other IDEs for Python e.g.,
More information about the Python-list