码迷,mamicode.com
首页 > 其他好文 > 详细

关于在2.7中出现 "UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal"

时间:2016-11-03 09:33:32      阅读:545      评论:0      收藏:0      [点我收藏+]

标签:using   warning   int   convert   with   文字   led   nic   back   

在中文字符串前面加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"

标签:using   warning   int   convert   with   文字   led   nic   back   

原文地址:http://www.cnblogs.com/qingyuanjushi/p/6025229.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!