从字典到组织 Excel
From dictionary to organized Excel
早上好,
我有一本字典:按以下方式整理;我想要做的是使用字典值作为键的列号。如下图:
我的第一个想法是遍历字典并创建一个文本文件,其中 dico_values = 制表符,然后将这个新文件转换为 excel 文件,但这似乎是一个太多的步骤。为大家干杯
你或许可以试试这个:
new_dico = {value: [] for value in dico.values()} # {1: [], 2: [], 3: [], ...}
for key, value in dico.items():
new_dico[value].append(key)
for otherkey in new_dico.keys():
if otherkey == value:
continue
else:
new_dico[otherkey].append("")
# new_dico == {1: ["President of ...", "Members of ..", "", ...], 2: ["", "", "President's Office", ...], ...}
# Then you can make a dataframe of 'new_dico' with pandas, for instance
早上好,
我有一本字典:按以下方式整理;我想要做的是使用字典值作为键的列号。如下图:
我的第一个想法是遍历字典并创建一个文本文件,其中 dico_values = 制表符,然后将这个新文件转换为 excel 文件,但这似乎是一个太多的步骤。为大家干杯
你或许可以试试这个:
new_dico = {value: [] for value in dico.values()} # {1: [], 2: [], 3: [], ...}
for key, value in dico.items():
new_dico[value].append(key)
for otherkey in new_dico.keys():
if otherkey == value:
continue
else:
new_dico[otherkey].append("")
# new_dico == {1: ["President of ...", "Members of ..", "", ...], 2: ["", "", "President's Office", ...], ...}
# Then you can make a dataframe of 'new_dico' with pandas, for instance