本文介绍了xlrd读取xls XLRDError:不支持的格式或文件损坏:预期的BOF记录;找到了'\ r \ n< html>'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码:

xls = open_workbook('data.xls')

作为回报:

File "/home/woles/P2/fin/fin/apps/data_container/importer.py", line 16, in import_data
  xls = open_workbook('data.xlsx')
File "/home/woles/P2/fin/local/lib/python2.7/site-packages/xlrd/__init__.py", line 435,     in open_workbook
ragged_rows=ragged_rows,
File "/home/woles/P2/fin/local/lib/python2.7/site-packages/xlrd/book.py", line 91, in open_workbook_xls
biff_version = bk.getbof(XL_WORKBOOK_GLOBALS)
File "/home/woles/P2/fin/local/lib/python2.7/site-packages/xlrd/book.py", line 1230, in getbof
bof_error('Expected BOF record; found %r' % self.mem[savpos:savpos+8])
File "/home/woles/P2/fin/local/lib/python2.7/site-packages/xlrd/book.py", line 1224, in bof_error
raise XLRDError('Unsupported format, or corrupt file: ' + msg)
XLRDError: Unsupported format, or corrupt file: Expected BOF record; found '\r\n<html>'

文件未损坏,我可以使用Excel,LibreOffice打开它.

The file is not damaged, I can open it with Excel, LibreOffice.

推荐答案

对于.xls文件,您可以使用read_excel():

For .xls files you can use read_excel():

df1= pd.read_excel("filename.xls")

参数headersep可以帮助您摆脱一些错误(在这里您可以找到有关参数的更多信息).用法示例

The parameters header and sep may help you get rid of some errors (here you can find more info on the parameters). An example of its usage

df2= pd.read_excel("filename.xls",  header = None, sep='delimiter')

注意,如果文件是.csv,则会出现错误

Note that if the files are a .csv you will get the error

要阅读.csv,需要使用read_csv(),就像这样

To read .csv one needs to use read_csv(), like this

df3= pd.read_csv("filename.csv")

这篇关于xlrd读取xls XLRDError:不支持的格式或文件损坏:预期的BOF记录;找到了'\ r \ n&lt; html&gt;'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 03:50