python 判断中文字符串是否相等

if ' 星期 节次' == tempStr[0]
pass

如何让等式有效,现在这种写法好像是永远无法成立的~!求大神!

第1个回答  2014-09-09
if u"星期 节次" == tempStr[0]追问

这样也是不可以的 我把tempStr[0]输出来了,里面有一样的字符串 但是还是进不去if

追答

把代码都发出来吧,我看看。

追问

追答

    读取csv的时候加上 .decode('utf-8')

    print tempstr[0]看看

追问

追答

py脚本和csv发给我,我调试下看看吧。

追问

求qq

追答#你的csv中使用的是gbk编码,在比较的时需要decode('gbk')才行
# -*- coding:utf-8 -*-
import csv
MajorclassIn = csv.reader(file('Computer.csv','rb'))  #以只读方式取得csv文件中内容
for row in MajorclassIn:   #行循环
        #print row
 tempstr=[i.strip() for i in row if i.strip()]
 if tempstr!=[]:
  if tempstr[0].decode('gbk')==u'专业班星期 节次':
   print tempstr
   print 'haha'

追问

其实就是编码的问题~ 感谢这位朋友~

本回答被提问者采纳
第2个回答  2017-12-04
试试
if ' 星期 节次' == tempStr[0].decode('gb2312')
pass

if ' 星期 节次' == tempStr[0].decode('utf-8')
pass