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

标签 python excel xlrd

我是 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/

相关文章:

python - Pyspark 将数据写入配置单元

Excel:Countif的Countif

python - 如何使用python根据单元格值获取Excel单元格行和列

python - 测试 Python 描述符

c++ - 将 OpenGL 代码从 C++ 移植到 Python

javascript - Django 开发服务器上的 Ajax 和 JQuery

excel - Excel 中的 if/And 语句和查找

excel - 将 'findnext' 函数合并到现有的 'find' 代码中?

python - 在Python中有一个元组列表,对于每个元组,希望将x[0]和x[1]放入Excel电子表格的A列和B列中

python - xlrd、xlwt、xlutils 如何在低级别与 Excel 配合使用?