Reputation: 981
I'm trying to run the command u'\xe1'.decode("utf-8")
in python and I get this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 0: ordinal not in range(128)
Why does it say I'm trying to decode ascii when I'm passing utf-8 as the first argument? In addition to this, is there any way I can get the character á
from u'\xe1'
and save it in a string?
Upvotes: 0
Views: 655
Reputation: 113998
decode
will take a string and convert it to unicode (eg: "\xb0".decode("utf8") ==> u"\xb0"
)
encode
will take unicode and convert it to a string (eg: u"\xb0".encode("utf8") ==> "\xb0"
)
neither has much to do with the rendering of a string... it is mostly an internal representation
try
print u"\xe1"
(your terminal will need to support unicode (idle will work ... dos terminal not so much))
>>> print u"\xe1"
á
>>> print repr(u"\xe1".encode("utf8"))
'\xc3\xa1'
>>> print repr("\xc3\xa1".decode("utf8"))
u'\xe1'
Upvotes: 1