How to encode and decode from spanish in python

六月ゝ 毕业季﹏ 提交于 2020-01-01 09:24:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!