-
Notifications
You must be signed in to change notification settings - Fork 12
UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode interpreting them as being unequal
Shuang0420 edited this page May 31, 2016
·
2 revisions
在使用xlrd时遇到的编码错误,其实可以推广,之后会写一篇python编码问题的总结。
fname = "测试结果日报05月10日.xlsx" sheetname="在线分析明细" bk = xlrd.open_workbook(fname) shxrange = range(bk.nsheets) try: sh = bk.sheet_by_name(sheetname) except: print "no sheet in %s named Sheet1" % fname
/usr/local/lib/python2.7/site-packages/xlrd/book.py:439: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal sheetx = self._sheet_names.index(sheet_name) no sheet in 测试结果日报05月10日.xlsx named Sheet1
请注意我在文件开头定义了编码方式
# -*- coding: utf-8 -*-
import sys reload(sys) sys.setdefaultencoding('utf8')
bk.sheet_by_name(sheetname)要求的sheetname是一个unicode的串, 而定义的sheetname是utf8,因此需要decode。
fname = "灵云测试结果日报05月10日.xlsx" sheetname="在线分析明细" bk = xlrd.open_workbook(fname) shxrange = range(bk.nsheets) try: sh = bk.sheet_by_name(sheetname.decode('utf8')) except: print "no sheet in %s named Sheet1" % fname
[[TOC]]