gpt4 book ai didi

python - 如何创建从 Excel 的行和列中提取的多个值的数组

转载 作者:太空宇宙 更新时间:2023-11-03 16:53:39 24 4
gpt4 key购买 nike

import xlrd

fw = 'C://Users/MM/Desktop/'

test=[]

wb1 = xlrd.open_workbook(fw + 'assignment.xls')
sh1 = wb1.sheet_by_index(0)

for a in range(2,sh1.nrows):
for b in range(0,sh1.ncols):
test.append(sh1.cell(a,b).value)
print(test)

我有这段代码,但它没有做我想要它做的事情。我得到的输出是

[0,200,clothes,1,300,shoes,2,900,food]

但是,我想要:

[0,200,clothes],[1,300,shoes],[2,900,food]

我想不出无论如何都能得到它。我想要的是从每一列读取每一行数据并将其放入上面的表单中的代码。 “0”是第 0 列,“200”是第 1 列,“衣服”是第 2 列。我确信这是从我的代码中知道的。

最佳答案

您可以使用嵌套 list comprehensions对于这样的事情。

test = [[sh1.cell(a,b).value for b in range(0, sh1.ncols)] for a in range(2, sh1.nrows)]

关于python - 如何创建从 Excel 的行和列中提取的多个值的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35637483/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com