comparing Unicode and string

John Roth JohnRoth1 at jhrothjr.com
Mon Oct 16 09:53:33 EDT 2006


luc.saffre at gmail.com wrote:
> Hello,
>
> here is something that surprises me.
>
>   #coding: iso-8859-1
>   s1=u"Frau Müller machte große Augen"
>   s2="Frau Müller machte große Augen"
>   if s1 == s2:
>       pass
>
> Running this code produces a UnicodeDecodeError:
>
> Traceback (most recent call last):
>   File "tmp.py", line 4, in ?
>     if s1 == s2:
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 6:
> ordinal not in range(128)
>
> I would have expected that "s1 == s2" gives True... or maybe False...
> but raising an error here is unnecessary. I guess that the comparison
> operator decides to convert s2 to a Unicode but forgets that I said
> #coding: iso-8859-1 at the beginning of the file.

The #coding declaration is not effective at runtime. It's
there strictly to guide the compiler in how to compile
byte strings.

The default encoding at run time is ascii unless
it's been set to something else, which is why the
error message specifies ascii.

John Roth

> 
> TIA for any comments.
> 
> Luc Saffre




More information about the Python-list mailing list