Python Unicode警告:Unicode相等比较无法将两个参数都转换为Unicode
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python Unicode警告:Unicode相等比较无法将两个参数都转换为Unicode,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2994字,纯文字阅读大概需要5分钟。
内容图文
![Python Unicode警告:Unicode相等比较无法将两个参数都转换为Unicode](/upload/InfoBanner/zyjiaocheng/750/431e31567df14732b10ef06bb206ec9b.jpg)
我很确定这是在我的代码部分中发生的情节:
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
我不确定为什么会出现这种错误.
解决方法:
我不能从你的例子中确切地说出错是什么,但是在将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
内容总结
以上是互联网集市为您收集整理的Python Unicode警告:Unicode相等比较无法将两个参数都转换为Unicode全部内容,希望文章能够帮你解决Python Unicode警告:Unicode相等比较无法将两个参数都转换为Unicode所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。