【发布时间】:2020-01-23 11:19:23
【问题描述】:
我正在尝试从 Excel 工作表中读取数据并在循环中形成字典,如下所示。
for row in range(2,sheet.max_row+1):
temp_list.clear()
for col in range (2,sheet.max_column):
temp_list.append(sheet.cell(row,col).value)
dict_key = sheet.cell(row,1).value
Create a dictionary
MyDataDictionary.update( {dict_key : temp_list})
假设我的 excel 有两行正在读取的数据,我的字典值总是被第二行的值覆盖
【问题讨论】:
-
不要使用
MyDataDictionary.update,你可以使用MyDataDictionary[dict_key] = temp_list,这将添加新的密钥。如果您使用列作为键名,那么您的数据当然会被覆盖,因为它具有相同的键名。
标签: python dictionary for-loop