Python Unicode警告:Unicode相等比较无法将两个参数都转换为Unicode

时间:2015-05-16 16:57:36

标签: python unicode matplotlib

我很确定这是在我的代码部分中出现的情节:

xlows = x[local_min]; xhighs = x[local_max]
ylows = y[local_min]; yhighs = y[local_max]
lowvals = prmsl[local_min]; highvals = prmsl[local_max]
# plot lows as blue L's, with min pressure value underneath.
xyplotted = []
# don't plot if there is already a L or H within dmin meters.
yoffset = 0.022*(m.ymax-m.ymin)
dmin = yoffset
for x,y,p in zip(xlows, ylows, lowvals):
    if x < m.xmax and x > m.xmin and y < m.ymax and y > m.ymin:
        dist = [np.sqrt((x-x0)**2+(y-y0)**2) for x0,y0 in xyplotted]
        if not dist or min(dist) > dmin:
            plt.text(x,y,'L',fontsize=14,fontweight='bold',
                    ha='center',va='center',color='b')
            plt.text(x,y-yoffset,repr(int(p)),fontsize=9,
                    ha='center',va='top',color='b',
                    bbox = dict(boxstyle="square",ec='None',fc=(1,1,1,0.5)))
            xyplotted.append((x,y))

我的源代码类似于this example从顶部开始的第三个。

追溯:

/Library/Python/2.7/site-packages/matplotlib/text.py:53: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
  if rotation in ('horizontal', None):
/Library/Python/2.7/site-packages/matplotlib/text.py:55: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
  elif rotation == 'vertical':

我打印了旋转值:

None
None
None
None
None
None
None
None
32.5360682877
/Library/Python/2.7/site-packages/matplotlib/text.py:53: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
  if rotation in ('horizontal', None):
/Library/Python/2.7/site-packages/matplotlib/text.py:55: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
  elif rotation == 'vertical':
32.5360682877
25.1125465842
25.1125465842
2.90036159155
2.90036159155
43.6364736689
43.6364736689

我不确定为什么会出现这种错误。

1 个答案:

答案 0 :(得分:8)

我无法从您的示例中确切地说出错误,但在将Unicode字符串与字节字符串进行比较时,Python 2.X中会出现此错误。 Python 2.X尝试使用默认的ascii编解码器将字节字符串隐式转换为Unicode。如果失败,由于字节字符串包含非ASCII字节,则会发出警告:

Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> u'pingüino' == 'pingüino'
__main__:1: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as
being unequal
False

Python 3.X通过不允许Unicode字符串文字中的非ASCII字符来减少混淆:

Python 3.3.5 (v3.3.5:62cf4e77f785, Mar  9 2014, 10:35:05) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 'pingüino' == b'pingüino'
  File "<stdin>", line 1
SyntaxError: bytes can only contain ASCII literal characters.

相反,程序员必须更明确,将字节与字节或Unicode与Unicode进行比较,或提供适当的转换:

>>> 'pingüino' == b'ping\xfcino'.decode('latin1')
True
>>> 'pingüino'.encode('latin1') == b'ping\xfcino'
True