str unicode

上一篇 / 下一篇  2018-05-25 14:36:32 / 个人分类:python

#coding=utf-8
import xlrd

import sys
print sys.getdefaultencoding()
file_location = "./TestCaseData.xlsx"
workbook = xlrd.open_workbook(file_location)
sheetLogin = workbook.sheet_by_index(0)
sheetRating = workbook.sheet_by_index(1)

# data = [[sheet.cell_value(r, c) for c in range(sheet.ncols)] for r in range(1, sheet.nrows)]
# print sheetRating.nrows

for i in sheetRating.row_values(0):
print type(i.encode("utf-8"))
# a = "业务类型"
# print type(a)
if "业务类型" in i.encode("utf-8"):
print "yes"
else:
print "no"



以上代码是在读取excel过程中,判断字符串是否在某行中的某个值
“业务类型”这几个字是,str类型,也就是utf-8的字符串编码
其中在遍历sheetRating.row_values(0)过程中,它的每个值,都是unicode的字符编码,
可以使用(type)来查看现在字符串是什么类型。

将str类型转换为unicode类型:   "业务类型"  ==》 u"业务类型"
将unicode类型转化为str类型:	i  ==> i.encode("utf-8")
解决这个问题,只需二选一即可。


TAG:

 

评分:0

我来说两句

Open Toolbar