问题
I have the following code written in python 2.7
# -*- coding: utf-8 -*-
import sys
_string = "años luz detrás"
print _string.encode("utf-8")
this throws the following error:
print _string.encode("utf-8")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128)
Any help appreciated, thanks in advance
回答1:
Add u
before the "
>>> _string = u"años luz detrás"
>>> print _string.encode("utf-8")
años luz detrás
This would do.
回答2:
In Python 2 a string literal ""
creates a bytestring. Then you call .encode("utf-8")
on a bytestring, Python tries first to decode it into Unicode string using a default character encoding (ascii
) before executing .encode("utf-8")
.
u""
creates Unicode string. It will fix the UnicodeDecodeError as @Bleeding Fingers suggested.
# -*- coding: utf-8 -*-
print u"años luz detrás"
It might lead to UnicodeEncodeError
if stdout is redirected. Set PYTHONIOENCODING
environment variable in this case.
来源:https://stackoverflow.com/questions/20935151/how-to-encode-and-decode-from-spanish-in-python