问题
I have a program that, when run in Python 2.7, produces proper Unicode output to the standard output. When run in Python 2.4, I get UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-4: ordinal not in range(128)
. What changed between version 2.4 and 2.7 that this works now?
回答1:
Although I could not find any mention of it elswhere, it appears that Python 2.7 is automatically converting text to the 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 (on another box)
> 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, an .encode("utf8") on the data before output should avoid the issue.
来源:https://stackoverflow.com/questions/7182384/why-do-i-get-a-ascii-encoding-error-with-unicode-data-in-python-2-4-but-not-in-2