在Python中比较字符串:字符串X是ASCII,字符串Y是UTF

Dja*_*cks 2 python unicode ascii utf-8 string-comparison

我有一个相当奇怪的问题:我如何比较字符串(使用Python),其中字符串X是ASCII,字符串Y是UTF还是Unicode?

目前,当我比较字符串时,我收到以下问题:

UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
  filteredtext = [t for t in s if t.lower() not in stopwords]
Run Code Online (Sandbox Code Playgroud)

如何确保字符串格式相同?

最好的祝福.

小智 6

使用底层编码转换所有字符串并进行比较:

print unicode(s1, 'ascii') == unicode(s1, 'utf-8')
Run Code Online (Sandbox Code Playgroud)