decode(..., errors='ignore') has no effect

Peter Otten __peter__ at web.de
Tue Jan 12 07:06:19 EST 2010


Jens Müller wrote:

> I try to decode a string,e.g.
> u'M\xfcnchen, pronounced [\u02c8m\u028fn\xe7\u0259n]'.decode('cp1252',
> 'ignore')
> but even thoug I use errors='ignore'
> I get UnicodeEncodeError: 'charmap' codec can't encode character u'\u02c8'
> in position 21: character maps to <undefined>
> 
> How come?

To convert unicode into str you have to *encode()* it.

u"...".decode(...) will implicitly convert to ASCII first, i. e. is 
equivalent to

u"...".encode("ascii").decode(...)

Hence the error message

...codec can't encode character u'\u02c8'...

Peter



More information about the Python-list mailing list