有时候我们用xlrd读取数据会出现错误,让我们一起看看怎么解决吧。
Python读取excel,常用第三方库是xlrd,写入的是xlwt。
在读取excel的时候,代码如下:
<p style="line-height: 1.75em"><span><a href="https://www.gaodaima.com/tag/data" title="查看更多关于data的文章" target="_blank">data</a> = xlrd.open_workbook('data.xls') <a href="https://www.gaodaima.com/tag/table" title="查看更多关于table的文章" target="_blank">table</a> = data.sheets()[0] nrows = table.nrows ncols = table.ncols<br></span></p>
www#gaodaima.com来源gaodaima#com搞(代@码网搞代码
会发现执行结果出现报错:
<p style="line-height: 1.75em"><span>File "E:Pythonlibsite-packagesxlrdcompdoc.py", line 426, in _locate_stream raise CompDocError("%s corruption: seen[%d] == %d" % (qname, s, self.seen[s])) xlrd.compdoc.CompDocError: Workbook corruption: seen[2] == 4<br></span></p>
如果在电脑上直接打开excel,excel是正常的。看报错信息,打开该文件,找到426行代码,代码如下:
<p style="line-height: 1.75em"><span>if self.seen[s]: print("_locate_stream(%s): seen" % qname, file=self.logfile); dump_list(self.seen, 20, self.logfile) raise CompDocError("%s corruption: seen[%d] == %d" % (qname, s, self.seen[s]))<br></span></p>
这是主动抛异常,将代码全部注释起来,再执行代码,发现读取正常。
以上就是Python中xlrd读取报错的解决办法。更多Python学习推荐:云海天Python教程网。
来源:搞代码网:原文地址:https://www.gaodaima.com