Lino Website

Problem reports

comparing Unicode and string

Monday, 16. October 2006 16:26.

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:

Running this code produces a UnicodeDecodeError:

Traceback (most recent call last):
  File "", 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.

(Also posted to comp.lang.python; see comments there.)

Copyright 2001-2007 Luc Saffre.
Generated 2007-06-07 16:22:51