gpt4 book ai didi

Python Unicode 警告 : Unicode equal comparison failed to convert both arguments to Unicode

转载 作者:太空宇宙 更新时间:2023-11-03 12:58:42 30 4
gpt4 key购买 nike

我很确定这发生在我要绘制文本的代码部分:

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

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

最佳答案

我不能从你的例子中确切地说出哪里出了问题,但是在 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

关于Python Unicode 警告 : Unicode equal comparison failed to convert both arguments to Unicode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30278393/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com