简体   繁体   English

使用xlrd从excel表导入python中的数字列表

[英]Import lists of numbers in python from excel sheets with xlrd

I'm an absolute beginner with python, and I want to use it mainly for producing 2D plots with matplotlib starting from excel data. 我是python的绝对初学者,我想主要用于从excel数据开始使用matplotlib生成2D绘图。

Let's suppose I have an excel sheet with the numbers 10, 20, 30, 40 in the first four rows of the first column; 假设我在第一列的前四行中有一个excel表,其中包含数字10,20,30,40; I want to create a python list with these numbers. 我想用这些数字创建一个python列表。

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? 有没有办法让它仍然使用xlrd?

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 参考: https//pythonhosted.org/xlrd3/cell.html

Particularly, you want the Cell.value property. 特别是,您需要Cell.value属性。

sheet.cell(row, col).value

将提供单元格中的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM