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

2017-05-10  本文已影响0人  FengBli
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
上一篇 下一篇

猜你喜欢

热点阅读