我有一个具有39列和10000行的Excel文件。
我想将键和值存储在列表中。
我只能想出
result = []
temp = {}
for x in range (1, 10000):
for y in range (1, 39):
# title and sheet also come from the excel
temp[ title[y] ] : SHEET.cell_value(x, y)
result.append(temp)
我编写的代码未按预期运行,因此如何重写它?
我希望结果是:
result = [
{'a':1, 'b':2, 'c':3, ...},
{'a':1, 'b':2, 'c':3, ...},
{'a':1, 'b':2, 'c':3, ...},
....
]
答案 0 :(得分:1)
使用熊猫,可能会更容易...
import pandas as pd
df = pd.read_excel(my_url)
print(df.to_dict('r'))
答案 1 :(得分:1)
我将使用熊猫。要从Cmd或终端安装首次运行,请执行以下操作:
pip install -U pandas
然后使用read_excel
导入excel工作表import pandas as pd
df = pd.read_excel("EXCEL SHEET PATH")
然后转到字典to_dict:
print(df.to_dict())
答案 2 :(得分:1)
正如其他人指出的那样,使用熊猫可能是行之有效的方法。但是,如果您希望使用普通的Python来实现解决方案,并假设已定义SHEET
和title
对象,则可能是您想要的:
result = []
for x in range (1, 10000):
temp = {}
for y in range (1, 39):
# title and sheet also come from the excel
temp[ title[y] ] = SHEET.cell_value(x, y)
result.append(temp)