【发布时间】:2021-04-21 18:42:51
【问题描述】:
我有一个非常具体的 Excel 表格,如下所示:
我正在尝试将其转换为这样的 Python 字典:
item_map = {
"1609755": {
"name": "test 1",
"price": 125,
"check_type": "check 1"
},
"1609756": {
"name": "test 2",
"price": 2500,
"check_type": "check 2"
},
"1609758": {
"name": "test 3",
"price": 2400,
"check_type": "check 3"
}
}
我的问题是:如何将 Excel 工作表转换为 Python 字典?
到目前为止,我尝试使用库 sheet2dict 和 pandas,但是在 excel 表中,id 是字典中的 key。结果,我很困惑如何确保第一列始终设置为键。
from sheet2dict import Worksheet
ws = Worksheet()
file_path = 'test.xlsx'
x = ws.xlsx_to_dict(path = file_path)
print(x)
【问题讨论】:
标签: python excel dictionary