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

3

我相信这是在我要绘制文本的代码部分发生的:

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))

我的源代码类似于这个例子中的第三个(从上往下数):http://matplotlib.org/basemap/users/examples.html 错误追踪:
/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

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


可能是[Python unicode equal comparison failed]的重复问题(https://dev59.com/rGMl5IYBdhLWcg3wsIyA)。 - Alastair Irvine
1个回答

9

从你的例子中,我无法确定出现了什么问题,但是在Python 2.X版本中,当比较Unicode字符串和字节串时会出现这个错误。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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接