我是 python 的绝对初学者,我想主要用它来使用 matplotlib 从 excel 数据生成二维图。
假设我有一个 Excel 工作表,第一列的前四行中有数字 10、20、30、40;我想用这些数字创建一个 python 列表。
我在尝试:
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)))
为了创建一个包含这些值的列表...但是当我尝试打印它时,我得到了
['number:10.0', 'number:20.0', 'number:30.0', 'number:40.0']
我该怎么做才能得到类似的东西
['10.0', '20.0', '30.0', '40.0']
以便它被识别为纯数字列表?有没有办法仍然使用 xlrd 来做到这一点?
最佳答案
怎么样
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
引用:https://pythonhosted.org/xlrd3/cell.html
特别是,您需要 Cell.value
属性。
关于python - 使用 xlrd 从 excel 表中导入 python 中的数字列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55272052/