使用 xlrd 从 excel 工作表中导入 python 中的数字列表
Import lists of numbers in python from excel sheets with xlrd
我是 python 的绝对初学者,我想主要用它来使用 matplotlib 从 excel 数据开始生成二维图。
假设我有一个 excel sheet,第一列的前四行的数字为 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
属性。
sheet.cell(row, col).value
将在单元格中提供值。
我是 python 的绝对初学者,我想主要用它来使用 matplotlib 从 excel 数据开始生成二维图。
假设我有一个 excel sheet,第一列的前四行的数字为 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
属性。
sheet.cell(row, col).value
将在单元格中提供值。