Python unicode 相等比较失败 [英] Python unicode equal comparison failed

查看:32
本文介绍了Python unicode 相等比较失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题链接到 在 Python 中搜索 Unicode 字符

我使用 python 编解码器读取 unicode 文本文件

codecs.open('story.txt', 'rb', 'utf-8-sig')

并试图在其中搜索字符串.但我收到以下警告.

UnicodeWarning: Unicode 相等比较未能将两个参数转换为 Unicode - 将它们解释为不相等

unicode 字符串比较有什么特别的方法吗?

解决方案

您可以使用 == 运算符来比较 unicode 对象的相等性.

<预><代码>>>>s1 = u'你好'>>>s2 = unicode("你好")>>>类型(s1),类型(s2)(<输入'unicode'>,<输入'unicode'>)>>>s1==s2真的>>>>>>s3='Hello'.decode('utf-8')>>>类型(s3)<输入'unicode'>>>>s1==s3真的>>>

但是,您的错误消息表明您没有比较 unicode 对象.您可能正在将 unicode 对象与 str 对象进行比较,如下所示:

<预><代码>>>>你'你好' == '你好'真的>>>你'你好' == 'x81x01'__main__:1: UnicodeWarning: Unicode 相等比较未能将两个参数转换为 Unicode - 将它们解释为不相等错误的

看看我是如何尝试将 unicode 对象与不代表有效 UTF8 编码的字符串进行比较的.

我想,您的程序正在将 unicode 对象与 str 对象进行比较,并且 str 对象的内容不是有效的 UTF8 编码.这似乎是因为您(程序员)不知道哪个变量保存 unicide,哪个变量保存 UTF8,哪个变量保存从文件中读取的字节.

我推荐 http://nedbatchelder.com/text/unipain.html,尤其是创建"Unicode 三明治."

This question is linked to Searching for Unicode characters in Python

I read unicode text file using python codecs

codecs.open('story.txt', 'rb', 'utf-8-sig')

And was trying to search strings in it. But i'm getting the following warning.

UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal

Is there any special way of unicode string comparison ?

解决方案

You may use the == operator to compare unicode objects for equality.

>>> s1 = u'Hello'
>>> s2 = unicode("Hello")
>>> type(s1), type(s2)
(<type 'unicode'>, <type 'unicode'>)
>>> s1==s2
True
>>> 
>>> s3='Hello'.decode('utf-8')
>>> type(s3)
<type 'unicode'>
>>> s1==s3
True
>>> 

But, your error message indicates that you aren't comparing unicode objects. You are probably comparing a unicode object to a str object, like so:

>>> u'Hello' == 'Hello'
True
>>> u'Hello' == 'x81x01'
__main__:1: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
False

See how I have attempted to compare a unicode object against a string which does not represent a valid UTF8 encoding.

Your program, I suppose, is comparing unicode objects with str objects, and the contents of a str object is not a valid UTF8 encoding. This seems likely the result of you (the programmer) not knowing which variable holds unicide, which variable holds UTF8 and which variable holds the bytes read in from a file.

I recommend http://nedbatchelder.com/text/unipain.html, especially the advice to create a "Unicode Sandwich."

这篇关于Python unicode 相等比较失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆