The Python 2.x print statement implicitly converts the `ʻunicodetype to the
str type and then outputs the standard output. The encoding for converting to ```unicode
-> str
is determined by locale.getpreferredencoding ()
.
↓ When I try to execute Python from Terminal on Mac.
>>> import locale
>>> locale.getpreferredencoding()
'UTF-8'
>>> print u'Ah ah'
Ah ah
↓ When I try to execute Python from the command prompt of Windows 7.
>>> import locale
>>> locale.getpreferredencoding()
'cp932'
>>> print u'Ah ah'
Ah ah
↓ When I try to execute Python from PyDev Console of Eclipse.
>>> import locale
>>> locale.getpreferredencoding()
'US-ASCII'
>>> print u'Ah ah'
Traceback (most recent call last):
File "<input>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
Since it was ascii in a special environment, ʻu'Ah'`` will result in a
ʻUnicodeEncodeError`` error.
If you dare to specify the encoding, you can do it by the following method.
>>> import sys, codecs
>>> sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
>>> print u'Ah ah'
Ah ah
It's very likely that you'll hide the bug ...
_ENC_LOCALE = locale.getpreferredencoding()
sys.stdout = codecs.getwriter(_ENC_LOCALE)(sys.stdout, errors='replace')
sys.stderr = codecs.getwriter(_ENC_LOCALE)(sys.stderr, errors='replace')
Recommended Posts