python读取excel数据出错


比如说我要读取 http://basic.10jqka.com.cn/600383/xls/Important_declaredate.xls
的数据信息,用的是xlrd程序包。


 import xlrd

xls= xlrd.open_workbook('Important_declaredate.xls',encoding_override='utf-8')
table=xls.sheets()[0]
print table

程序debug错误信息:


 File "/Volumes/MacHD/work/stock/main.py", line 20, in main
    xls= xlrd.open_workbook('Important_declaredate.xls',encoding_override='utf-8')
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/xlrd/__init__.py", line 435, in open_workbook
    ragged_rows=ragged_rows,
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/xlrd/book.py", line 91, in open_workbook_xls
    biff_version = bk.getbof(XL_WORKBOOK_GLOBALS)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/xlrd/book.py", line 1258, in getbof
    bof_error('Expected BOF record; found %r' % self.mem[savpos:savpos+8])
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/xlrd/book.py", line 1252, in bof_error
    raise XLRDError('Unsupported format, or corrupt file: ' + msg)
xlrd.biffh.XLRDError: Unsupported format, or corrupt file: Expected BOF record; found '\xd6\xb8\xb1\xea\\\xc8\xd5\xc6'

为什么会出现错误?

python 编码 excel

wxh522 11 years, 8 months ago

自己各种尝试,发现是xls文件的错误,不知道内部的内容的编码有错误。


 file="Important_declaredate.xls"
f = open(file, 'rb')
lines = f.readlines()
for line in lines:
    line = line.decode('gb2312').encode('utf8') 
    print line

有些时候多引用第三方程序,反而造成不必要的麻烦。有可能是对应的xls文件格式的错误。

HIDEO answered 11 years, 8 months ago

Your Answer