美文网首页
python笔记--(6)--[UTF-8字符比较]

python笔记--(6)--[UTF-8字符比较]

作者: FengBli | 来源:发表于2017-05-10 00:36 被阅读0次
city = u'北京'
city_name = open(file_name, 'r').readline()    # 文件中只有“北京”二字,且以UTF-8保存

若直接使用二者比较

if city == city_name:
  pass

UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal

Right way

if city.decode('utf-8) == city_name:
  pass

相关文章

网友评论

      本文标题:python笔记--(6)--[UTF-8字符比较]

      本文链接:https://www.haomeiwen.com/subject/drcwtxtx.html