使用xlrd读取本地Excel数据触发TypeError,如何修复?
I'm trying to read data from a local Excel file and print it, but I keep getting an error. Here's my code:
import xlrd from xlrd import xldate_as_tuple import datetime data1 = xlrd.open_workbook(r'D:\test.xlsx') table = data1.sheets()[0] tables = [] def import_excel(excel): for test in range(excel.nrows): array = [table.cell_value(test, 0), table.cell_value(test, 1), table.cell_value(test, 2), table.cell_value(test, 3), table.cell_value(test, 4)] tables.append(array) if __name__ == '__main__': import_excel(table) for i in tables: # pass print(i) num1 = tables[0] num2 = tables[1] num3 = tables[2] num4 = tables[3] num5 = tables[4] nu1 = 1 while nu1 < num2: print("%d\t%d\t%d\t%d\t%d" % (nu1, num2, num3, num4, num5)) nu1 = nu1 + 1
The error I'm getting:
TypeError: '<' not supported between instances of 'int' and 'list'
How can I fix this?
Let's break down what's going wrong here and fix it step by step.
The Root Cause
Looking at your code, when you do num2 = tables[1], you're assigning an entire row (a list) to num2—not a single value from that row. Then in the while loop, you try to compare nu1 (an integer) to num2 (a list), which Python doesn't allow, hence the TypeError.
How to Fix It
I assume you want to use a specific value from each row (like the second column, index 1) for your loop condition and printout. Here's how to adjust your code:
- Extract individual values from each row list instead of assigning the whole row to variables like
num2,num3, etc. - Ensure the values are integers (since you're using them in integer comparisons and formatting with
%d). xlrd'scell_valuemight return floats even for whole numbers, so we can cast them toint. - Clean up variable names to make the code more readable (optional but recommended).
Here's the revised code:
import xlrd from xlrd import xldate_as_tuple import datetime data1 = xlrd.open_workbook(r'D:\test.xlsx') table = data1.sheets()[0] tables = [] def import_excel(excel): for row_idx in range(excel.nrows): # Extract values from columns 0-4, cast to int (adjust if you have non-integer data) row_values = [ int(table.cell_value(row_idx, 0)), int(table.cell_value(row_idx, 1)), int(table.cell_value(row_idx, 2)), int(table.cell_value(row_idx, 3)), int(table.cell_value(row_idx, 4)) ] tables.append(row_values) if __name__ == '__main__': import_excel(table) # Print all rows first (optional) for row in tables: print(row) # Extract specific values from rows (adjust indices based on your actual data needs) # Example: if you want the second value (column index 1) from each row: row1_val = tables[0][1] row2_val = tables[1][1] row3_val = tables[2][1] row4_val = tables[3][1] row5_val = tables[4][1] nu1 = 1 # Now compare nu1 to the integer value row2_val, not the whole list while nu1 < row2_val: # Print the integer values, not the lists print("%d\t%d\t%d\t%d\t%d" % (nu1, row2_val, row3_val, row4_val, row5_val)) nu1 += 1
Additional Notes
- If your Excel file has non-integer data (like dates or strings), remove the
int()cast and adjust the formatting string (e.g., use%sfor strings, handle dates withxldate_as_tupleif needed). - Make sure the rows you're accessing (
tables[0]totables[4]) actually exist in your Excel sheet—if your sheet has fewer than 5 rows, you'll get anIndexErrornext. Add a check likeif len(tables) >=5:before accessing those indices to avoid crashes.
内容的提问来源于stack exchange,提问作者DaMeilao




