首页 > 其他分享 > UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting

UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting

时间:2022-12-25 16:11:34浏览次数:53  
标签:comparison convert 请求 稍后 content dict Unicode print

执行python脚本时,第102行报错如下:

/home/tools/JS_SRVC_ID_Reset_SMS_linux.py:102:

UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
if content_dict['msg'] == '该号码还有相同类型请求未处理完,本次请求将稍后执行':

 

程序原文如下:

content_dict = json.loads(response.text)
if content_dict['msg'] =='该号码还有相同类型请求未处理完,本次请求将稍后执行':
    print('该号码还有相同类型请求未处理完,本次请求将稍后由系统自动重试,请勿再人工下发相同指令!')
exit()
else:
print('开关指令已发送...')



原因是 运行的python系统为2.6.6 版本, 字符串编码不一致造成的。

解决方法1: 使用python3 版本去运行,不会再报错。
解放方法2: 对要比比较的中文字符串进行解码成 utf-8格式, 程序如下改写:
      
content_dict = json.loads(response.text)
if content_dict['msg'] =='该号码还有相同类型请求未处理完,本次请求将稍后执行'.decode('utf-8'):
    print('该号码还有相同类型请求未处理完,本次请求将稍后由系统自动重试,请勿再人工下发相同指令!')
exit()
else:
print('开关指令已发送...')
或者如下使用变量:
content_dict = json.loads(response.text)
label='该号码还有相同类型请求未处理完,本次请求将稍后执行'
if content_dict['msg'] ==label.decode('utf-8'):
    print('该号码还有相同类型请求未处理完,本次请求将稍后由系统自动重试,请勿再人工下发相同指令!')
exit()
else:
print('开关指令已发送...')
 
 

标签:comparison,convert,请求,稍后,content,dict,Unicode,print
From: https://www.cnblogs.com/tonyxiao/p/17004150.html

相关文章