[UnicodeEncodeError] Don't know what else to try

"Martin v. Löwis" martin at v.loewis.de
Fri Nov 14 05:01:27 EST 2008


>     print output.decode('utf-8')
>   File "C:\Python25\lib\encodings\utf_8.py", line 16, in decode
>     return codecs.utf_8_decode(input, errors, True)
> UnicodeEncodeError: 'ascii' codec can't encode character u'\xe2' in
> position 47:
>  ordinal not in range(128)

Notice that it complains about the 'ascii' codec, when you were using
the utf-8 codec. Also, it complains about *en*coding, when you try
decoding.
For this, there could be two possible causes:

1. output is already a Unicode object - decoding it is not a meaningful
   operation. So Python first *en*codes it with ascii, then would decode
   it with UTF-8. The first step fails.
2. decoding from utf-8 works fine. It then tries to print output, which
   requires an encoding. By default, it encodes as ascii, which fails.

> ========
> 
> Here's the code:
> ========
> 		try:
> 			output = "Different: (%s) %s : %s # %s" %
> (id[2],id[3],id[0],id[1])

Add
                        print type(output)
here. If it says "unicode", reconsider the next line

> 			print output.decode('utf-8')

Regards,
Martin



More information about the Python-list mailing list