首页 > 代码库 > 关于在2.7中出现 "UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal"
关于在2.7中出现 "UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal"
在中文字符串前面加u.
Make sure your code is in UTF-8 (NOT Latin-1) and/or use a coding line as so:
#! /usr/bin/python # -*- coding: utf-8 -*- a = {"a": u"??"} b = "??" assert b == a[‘a‘] assert b.decode(‘utf-8‘) == a[‘a‘].decode(‘utf-8‘)
If you‘re using unicode across the board, you can import unicode_literals from the future and cut back on encoding heartaches:
#! /usr/bin/python # -*- coding: utf-8 -*- from __future__ import unicode_literals a = {"a": u"??"} b = "??" assert b == a[‘a‘] assert b == a[‘a‘] assert b.encode(‘utf-8‘) != a[‘a‘] assert b.encode(‘utf-8‘) == a[‘a‘].encode(‘utf-8‘)
If a file uses unicode_literals, all "strings" are now u"unicode" objects (per the coding of the file) if they‘re not b"prepended" with a b (to emulate the string/bytes split in Python 3.X).
关于在2.7中出现 "UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal"
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。