python2.7运行出现的Warning: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode

运行出现如下错误

uncode编码警告:在unicode等价比较中,把两个参数同时转换为unicode编码失败。中断并认为他们不相等。

windows下的字符串str默认编码是ascii,而python编码是utf8

解决方法:添加如下几行代码

import sys  
  
reload(sys)  
sys.setdefaultencoding('utf8')  

或者
if sys.getdefaultencoding() != 'utf-8':
reload(sys)
sys.setdefaultencoding('utf-8')

该解决方法同样适用于如下报错

ascii' codec can't decode byte 0xe6 in position 31: ordinal not in range(128)

  

原文地址:https://www.cnblogs.com/robinunix/p/9019874.html