Search code examples
pythonexcelxlrd

TypeError: 'int' object is not subscriptable using xlrd


I would like to read out data from excel sheet using xlrd. I want to go through specific cells from the excel and if I didn't find the number "1" I want to add +1 to the row number and start again. My problem is that when I specify the cell in the for loop I get a TypeError:'int' object is not subscriptable.

import xlrd

Excelsheet1 = "info_2020.xlsx" 
Book1 = xlrd.open_workbook(Excelsheet1) 
first_sheet = Book1.sheet_by_index(9)

row_num = 1

for look_for_one in first_sheet.row_values(row_num[12]):
        if look_for_one == 1:
            print(first_sheet.row_values(row_num)[25])
        else:
            row_num += 1

Anyone knows what is wrong? Thank you!


Solution

  • It looks like you forgot a parenthesis and wrote first_sheet.row_values(row_num[12]) instead of first_sheet.row_values(row_num)[12].

    Thus the error message is "int is not subscriptable" which means "I don't understand row_num[12] because row_num is an int (a number), not a list."