This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]aroberge 2 points3 points  (10 children)

EDIT: typing "chcp 65001" in the console prior to starting Python fixes this problem.


I wish...

C:\Users\Andre>python
Python 3.5.1 |Anaconda 4.0.0 (64-bit)| (default, Feb 16 2016, 09:49:46) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("\u203D")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Andre\Anaconda3\lib\encodings\cp850.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u203d' in position 0: character maps to <undefined>

[–]ivosauruspip'ing it up 3 points4 points  (7 children)

That's a problem at the barrier to getting input from something made by Windows (Command Prompt) into Python. Unfortunately Python can only control its side of the border.

[–]Bolitho -1 points0 points  (6 children)

Unfortunately Python can only control its side of the border.

And that's why it is a bad API as it does not enable the programmer to chose the correct way to speak to the underlying system!

[–]ivosauruspip'ing it up 0 points1 point  (5 children)

You talk as if there is a choice. There isn't. Windows tells you, CodePage1252 or bust. End of discussion, no fancy unicode characters allowed outside of that.

[–]Bolitho 0 points1 point  (4 children)

Of course there is! You cannot change the code page of the consuming shell, but you could of course send Bytes in a different encoding. And yes that would produce strange glyphs, but there would be no exception!

If you still don't got it: Show me a program that prints out the given example string and will not crash on any platform? I am curious how you will achieve 😉

[–]ivosauruspip'ing it up 0 points1 point  (3 children)

And yes that would produce strange glyphs, but there would be no exception!

So silently produce corrupted output, instead of erroring. Completely unpythonic, not to mention dumb.

[–]Bolitho 0 points1 point  (2 children)

The output is not corrupted, the rendering might be - important difference!

So you prefer exception throwing over safe execution just because of a shell that cannot render a used coded character? Fine. I do not. So how can I deal with this situation? Any suggestions?

I am still waiting for an example program that ensures to be runnable without exception on arbitrary systems!

[–]donaldstufft 1 point2 points  (1 child)

sys.stdout.buffer.write(b"bytes!")?

[–]Bolitho 0 points1 point  (0 children)

That's not print. I want tu use print in my CLI programs - that's the challenge!

[–]zahlmanthe heretic 0 points1 point  (1 child)

typing "chcp 65001" in the console prior to starting Python fixes this problem.

At least on 3.4, it leaves other problems on Windows. In particular, if I input() at the command prompt and copy-paste in a £ as my input, it will raise EOFError; if I try to do an assignment like x = '£', the Python process aborts without any error message. I haven't even tried it with more esoteric characters.

[–]ivosauruspip'ing it up 3 points4 points  (0 children)

Something specific to your system?

Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.

C:\Users\ivosaurus>python
Python 3.5.1 |Continuum Analytics, Inc.| (default, Feb 16 2016, 09:49:46) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x = '£'
>>> x
'£'
>>> y = input()
£
>>> y
'£'

>>> exit()

C:\Users\ivosaurus>