Search code examples
pythonxlrd

Using xlrd in python to get integers from excel for my list but the integers are returned as (number:integer)


I am trying to pull integers from my exceltest.xlsx file. The integers are in column A of the file (row 2 = 20, row 3 = 30, row 4 = 40). When I run the code below I get the following in list1: [number:20.0, number:30.0, number:40.0]. How do I get it to return [20, 30, 40] instead? I am trying to write list1 into an excel file and it doesn't work when list1 includes the term "number." I've successfully written lists and tuples that I defined in my code into Excel, but am struggling in the case where I've pulled integers from one Excel file to write into another Excel file. Thank you.

book = open_workbook("exceltest.xlsx")
sheet = book.sheet_by_index(0)
list1 = []

for row in range(1,4): 
    list1.append(sheet.cell(row,0))

Solution

  • Replace

    list1.append(sheet.cell(row, 0))
    

    with

    list1.append(sheet.cell(row, 0).value)
    

    You could also look into replacing the 'for' loop with something like this.

    allrows = [sheet.row_values(i)[0] for i in range(sheet.nrows) if sheet.row_values(i)[0]]