Home » excel » Import lists of numbers in python from excel sheets with xlrd

Import lists of numbers in python from excel sheets with xlrd

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m an absolute beginner with python, and I want to use it mainly for producing 2D plots with matplotlib starting from excel data.

Let’s suppose I have an excel sheet with the numbers 10, 20, 30, 40 in the first four rows of the first column; I want to create a python list with these numbers.

I was trying:

from xlrd import open_workbook

book = open_workbook('filepathname')
sheet = book.sheet_by_index(0)

list = []
for row in range(0,4):
   list.append(str(sheet.cell(row,0)))

in order to create a list with such values… but when I try to print it, I get

['number:10.0', 'number:20.0', 'number:30.0', 'number:40.0']

how can I do, instead, to get something like

['10.0', '20.0', '30.0', '40.0']

so that it is recognized as a list of pure numbers? Is there a way for doing it still using xlrd?

How to&Answers:

How about

from xlrd import open_workbook

book = open_workbook('filepathname')
sheet = book.sheet_by_index(0)

list = []
for row in range(0,4):
    # Remove str(...) if you want the values as floats.
    list.append(str(sheet.cell(row,0).value))  # Changed this line

Reference: https://pythonhosted.org/xlrd3/cell.html

Particularly, you want the Cell.value property.

Answer:

sheet.cell(row, col).value

will provide the value in the cell.