Why am I getting an ASCII coding error with Unicode data in Python 2.4, but not 2.7?

I have a program that, when launched in Python 2.7, outputs the correct Unicode output to standard output. When launched in Python 2.4, I get a UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-4: ordinal not in range(128) . What has changed between versions 2.4 and 2.7, does it work now?

+4
source share
1 answer

Although I could not find a mention of this in elswhere, it seems that Python 2.7 will automatically convert the text to a terminal encoding, instead of throwing an error as expected.

Python 2.7:

 > echo $LANG en_US.UTF-8 > python -c 'import sys; print sys.getdefaultencoding()' ascii > python -c 'import sys; sys.stdout.write(u"\u03A3")' Ξ£ > python -c 'import sys; sys.stdout.write(u"\u03A3".encode("utf8"))' Ξ£ 

Python 2.6 (in another window)

 > echo $LANG en_US.UTF-8 > python -c 'import sys; print sys.getdefaultencoding()' ascii > python -c 'import sys; sys.stdout.write(u"\u03A3")' Traceback (most recent call last): File "<string>", line 1, in <module> UnicodeEncodeError: 'ascii' codec cant encode character u'\u03a3' in position 0: ordinal not in range(128) > python -c 'import sys; sys.stdout.write(u"\u03A3".encode("utf8"))' Ξ£ 

In any case, .encode ("utf8") for the data should be avoided before exiting.

+7
source

Source: https://habr.com/ru/post/1368860/


All Articles