Dr. Dobb's Python-URL! - weekly Python news and links (Dec 30)

Thomas Heller theller at python.net
Tue Jan 4 13:57:11 EST 2005


Max M <maxm at mxm.dk> writes:

> Thomas Heller wrote:
>
>> It seems also the error messages aren't too helpful:
>>
>>>>>"ä".encode("latin-1")
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in ?
>> UnicodeDecodeError: 'ascii' codec can't decode byte 0x84 in position 0: ordinal not in range(128)
>> Hm, why does the 'encode' call complain about decoding?
>
> Because it tries to print it out to your console and fail. While
> writing to the console it tries to convert to ascii.

Wrong, same error without trying to print something:

>>> x = "ä".encode("latin-1")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
UnicodeDecodeError: 'ascii' codec can't decode byte 0x84 in position 0: ordinal not in range(128)
>>>

>
> Beside, you should write:
>
> u"ä".encode("latin-1") to get a latin-1 encoded string.

I know, but the question was: why does a unicode string has a encode
method, and why does it complain about decoding (which has already been
answered in the meantime).

Thomas



More information about the Python-list mailing list