compare unicode to non-unicode strings

Fredrik Lundh fredrik at pythonware.com
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
's\xc3\xa9d'
 >>> u
u's\xe9d'
 >>> unicode(b, "utf-8")
u's\xe9d'
 >>> unicode(b, "utf-8") == u
True

</F>




More information about the Python-list mailing list