2

这将返回False,因为第一个是Str对象,第二个是Unicode对象。

但是,由于它们都是Ramón,我该怎么做才能返回True

我试图将第一个转换为 Unicode 对象:

>>> varString = 'Ramón'
>>> varUnicode = u'Ramón'
>>> varString == varUnicode
False
>>> newUnicode == unicode(varString, encoding='unicode-escape')
>>> varString; varUnicode; newUnicode
'Ram\xa2n'
u'Ram\xf3n'
u'Ram\xa2n'
>>> varUnicode == newUnicode
False

它们有不同的编码。我能做些什么?谢谢!

4

1 回答 1

1

varString is unlikely to be encoded in unicode-escape. The Python interpreter uses the encoding of sys.stdin.encoding when it decodes what it reads at the >>> prompt into a unicode object. So you can use the same encoding when you decode your str object for yourself:

>>> import sys
>>> sys.stdin.encoding
'UTF-8'
>>> varString = 'Ramón'
>>> varUnicode = u'Ramón'
>>> newUnicode = unicode(varString, encoding='UTF-8') # or encoding=sys.stdin.encoding
>>> varString; varUnicode; newUnicode
'Ram\xc3\xb3n'
u'Ram\xf3n'
u'Ram\xf3n'
>>> varUnicode == newUnicode
True
于 2012-09-18T18:19:21.947 回答