[UnicodeEncodeError] Don't know what else to try

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Sat Nov 15 17:11:43 CET 2008

On Sat, 15 Nov 2008 14:12:42 +0100, Gilles Ganault wrote:

> On Fri, 14 Nov 2008 17:39:00 +0100, "Martin v. Löwis"
> <martin at v.loewis.de> wrote:
>>Can you first please report what happened when you add the print
> Thanks guys, I found how to handle this:
> ===========
> for id in rows:
> 	#Says Unicode, but it's actually not
> 	#print type(id[1])
> 	#<type 'unicode'>

If it says `unicode` *it is* `unicode`.

> 	try:
> 		print id[1];
> 	except UnicodeEncodeError:
> 		print "Not unicode"

But it *is* `unicode` if `type()` says so!

Your code still fails when ``id[1]`` can't be encoded in `sys.encoding`, 
'iso8859-15', or 'cp1252'.  Even worse: The output may be even encoded in 
different encodings this way.  That's garbage you can't decode properly 
with one encoding anymore.

A clean solution would be just one ``print`` with a call of `encode()` 
and an explicit encoding.  I'd use 'utf-8' as default but give the user 
of the program a possibility to make another choice.

	Marc 'BlackJack' Rintsch

More information about the Python-list mailing list