Pandas 0.19.2 read_excel IndexError:列表索引超出范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43395840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:23:21  来源:igfitidea点击:

Pandas 0.19.2 read_excel IndexError: List index out of range

pythonexcelpandas

提问by 10SecTom

I am looking to parse an excel spreadsheet. I decided to use pandas but got caught by an error straight off the bat.

我正在寻找解析 excel 电子表格。我决定使用 Pandas,但立即被错误捕获。

enter image description here

在此处输入图片说明

I started with the code below but played around with using a full path and also tried setting the sheetname.

我从下面的代码开始,但尝试使用完整路径并尝试设置工作表名称。

import pandas as pd

table = pd.read_excel('ss_12.xlsx')

if __name__ == '__main__':
    pass

The excel spreadsheet is in the same directory as my script file. I taught it would work the same as open() in this sense, just a name required if its in the same directory. I have looked at a few examples online and going by them this should work.

excel 电子表格与我的脚本文件位于同一目录中。我教过它在这个意义上与 open() 一样工作,如果它在同一目录中,则只需要一个名称。我在网上看了一些例子,通过它们应该可以工作。

enter image description here

在此处输入图片说明

I am trying to strip the first column seen in the image above. The full error (not sure how to format it, sorry)

我试图去除上图中看到的第一列。完整错误(不知道如何格式化,抱歉)

C:\xx\Playpen\ConfigList_V1_0.xlsx
Traceback (most recent call last):
  File "C:\xx\Playpen\getConVars.py", line 12, in <module>
    pd.read_excel(excelFile)
  File "C:\xx\Programs\Python\Python35\lib\site-packages\pandas\io\excel.py", line 200, in read_excel
    **kwds)
  File "C:\xx\Programs\Python\Python35\lib\site-packages\pandas\io\excel.py", line 432, in _parse_excel
    sheet = self.book.sheet_by_index(asheetname)
  File "C:\xx\Programs\Python\Python35\lib\site-packages\xlrd\book.py", line 432, in sheet_by_index
    return self._sheet_list[sheetx] or self.get_sheet(sheetx)
IndexError: list index out of range

回答by ZMan

Make sure you have the right kind of Excel spreadsheet. I had this same error and realized that I had saved it as a Strict XML Open Spreadsheet which still had the .xlsx extension.

确保您拥有正确类型的 Excel 电子表格。我遇到了同样的错误,并意识到我已将其保存为仍然具有 .xlsx 扩展名的 Strict XML Open Spreadsheet。

回答by splinter

If you just want to read the file, it's better to use os.pathas follows:

如果你只是想读取文件,最好使用os.path如下:

import os
import pandas as pd

dir = 'path_to_excel_file_directory'
excelFile = os.path.join(dir, 'fileName.xlsx')
pd.read_excel(excelFile)

And if the excel file is in the same directory as your script, you can use inspectto automatically detect the directory it's in:

如果 excel 文件与您的脚本在同一目录中,您可以使用它inspect来自动检测它所在的目录:

scriptName = inspect.getframeinfo(inspect.currentframe()).filename
dir = os.path.dirname(os.path.abspath(filename))
excelFile = os.path.join(dir, 'fileName.xlsx')
pd.read_excel(excelFile)

One final note: the part

最后一个注意事项:部分

if __name__ == '__main__':
    pass

is not related to the question.

与问题无关。

回答by Luca1988

Quick solution: create a brand new file (.xls) with the content of the one that gives you the error. It worked for me.

快速解决方案:创建一个全新的文件 (.xls),其中包含出现错误的文件的内容。它对我有用。