import xlrd
def get_data(a):
aa = worksheet.cell(a, 0).value
bb = int(worksheet.cell(a, 2).value)
cc = worksheet.cell(a, 3).value
return([aa,bb,cc])
workbook = xlrd.open_workbook('d:/test.xls')
worksheet = workbook.sheet_by_index(0)
c = 1
objs = []
while worksheet.row(worksheet.nrows-1):
f = get_data(c)
objs.append(f)
c = c + 1
print(objs)
我收到以下错误:
Traceback (most recent call last): File "D:\test.py", line 20, in <module> f = get_data(c) File "D:\test.py", line 6, in get_data aa = worksheet.cell(a, 0).value File "C:\Python34\lib\site-packages\xlrd\sheet.py", line 399, in cell self._cell_types[rowx][colx], IndexError: list index out of range
答案 0 :(得分:0)
I see the problem now. The error occurs when you call get_data(c)
, and in that function it calls worksheet.cell(a, 0)
. While accessing column 0 of some row (the row number is not given in the error message), the index of either the row or column (or both) is greater than the data that exists.
Since the column number, 0
, is a constant, the greater question is on what row is it failing? One way to approach debugging this is to catch the exception and log the row number. For example:
try:
get_data(c)
except Exception as err:
print("Failed to get data for row", c)
raise
This change will tell you what row fails.
In reading your code more closely to try and determine the source of the row number, I see that your loop never ends.
while worksheet.row(worksheet.nrows-1):
If the row object you retrieve is "truthy" then the loop will repeat. Instead you want to check when your row number has reached the limit of rows in the file. The most natural way to do this is with a 'for' loop.
for c in range(1, worksheet.nrows+1):
f = get_data(c)
objs.append(f)
This naturally makes the loop finite, and also eliminates two extra lines of code you have (no need to initialize or increment c).