compare unicode to non-unicode strings

Fredrik Lundh fredrik at
Sun Aug 31 15:42:47 CEST 2008

Asterix wrote:

> how could I test that those 2 strings are the same:
> 'séd' (repr is 's\\xc3\\xa9d')
> u'séd' (repr is u's\\xe9d')

determine what encoding the former string is using (looks like UTF-8), 
and convert it to Unicode before doing the comparision.

 >>> b = 's\xc3\xa9d'
 >>> u = u's\xe9d'
 >>> b
 >>> u
 >>> unicode(b, "utf-8")
 >>> unicode(b, "utf-8") == u


More information about the Python-list mailing list