gpt4 book ai didi

Python:如何将嵌套字典打印为具有可变行的复杂表?

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

假设我有这个嵌套字典:

d = {'Ben': {'wo_mana': ['Strength = 1.10', 'Speed = 1.50'], 'wi_mana': ['Int = 1.20'], 'Skill': 'true', 'Magic': 'false'},
'Tom': {'wi_mana': ['Int = 1.40'], 'wo_mana': ['Agility = 1.60'], 'Skill': 'true', 'Magic': 'true'}}

键和值不是我自己定义的,它们实际上是从文件中提取的数据。

SkillMagic 的值的数量是固定的,即 1。

但是,wo_manawi_mana的取值个数不固定,可以是1、2、3、4等。

在上面的示例中,wo_mana 对于 Ben 有 2 个值。

我的目标是将其打印在表格中(用选项卡分隔),当在 Excel 中打开时,它看起来像这样:

Name Skill Magic wo_mana          wi_mana
Ben true false Strength = 1.10 Int = 1.20
Speed = 1.50
Tom true true Agility = 1.60 Int = 1.40

请注意,对于每个人,wo_manawi_mana 列可以有多行。

我尝试使用csv模块,我的代码:

import csv, itertools

header = ['Name', 'Skill', 'Magic', 'wo_mana', 'wi_mana']

with open('output.csv', 'w') as f:
w = csv.DictWriter(f, header)
w.writeheader()
for key, val in sorted(d.items()):
row = {'Name': key}
row.update(val)
w.writerow(row)

我的输出:

Name,Skill,Magic,wo_mana,wi_mana
Ben,true,false,"['Strength = 1.10', 'Speed = 1.50']",['Int = 1.20']
Tom,true,true,['Agility = 1.60'],['Int = 1.40']

所以看起来不适合使用csv打印多行部分

那么也许我必须使用字符串格式?有谁知道如何打印这样的表格?任何帮助将不胜感激。

最佳答案

给你(已编辑):

import csv, itertools
d = {'Ben': {'wo_mana': ['Strength = 1.10'], 'wi_mana': ['Int = 1.20'], 'Skill': 'true', 'Magic': 'false'},
'Tom': {'wi_mana': ['Int = 1.40'], 'wo_mana': ['Agility = 1.60'], 'Skill': 'true', 'Magic': 'true'}}


header = ['Name', 'Skill', 'Magic', 'wo_mana', 'wi_mana']

with open('output.csv', 'w') as f:
w = csv.DictWriter(f, header)
w.writeheader()
for key, val in sorted(d.items()):
row = {'Name': key}
# print val


wo_mana = [item for item in val['wo_mana']]
wi_mana = [item for item in val['wi_mana']]
val['wo_mana'] = wo_mana[0]
val['wi_mana'] = wi_mana[0]

row.update(val)
w.writerow(row)
dict_list = []
if len(wi_mana) > len(wo_mana):

for index, item in enumerate(wo_mana):
dict_ = {}
dict_['wi_mana'] = wi_mana[index]
dict_['wo_mana'] = wo_mana[index]
# print dict_

dict_list.append(dict_)
[dict_list.append({'wi_mana': item}) for item in wi_mana[len(wo_mana):]]
else:

for index, item in enumerate(wi_mana):
dict_ = {}
dict_['wi_mana'] = wi_mana[index]
dict_['wo_mana'] = wo_mana[index]
# print dict_
dict_list.append(dict_)
[dict_list.append({'wo_mana': item}) for item in wo_mana[len(wi_mana):]]
# print dict_list
if len(dict_list) > 1:
[w.writerow(dict_) for dict_ in dict_list[1:]]

这有效!让我知道。

编辑:根据问题:

However, the number of values for wo_mana and wi_mana is unfixed, which can be 1, 2, 3, 4 etc.

所以我假设不会出现 KeyError。根据评论中的修改语句这里是解决方案:

它没有针对各种条件进行正确测试,根据我从评论中了解到的内容,它工作正常,如果有任何问题,请告诉我:

import csv, itertools
d = {'Ben': {'Skill': 'true', 'Magic': 'false'},
'Tom': {'wo_mana': ['Strength = 1.10'], 'wi_mana': ['Int = 1.20', 'O_o'], 'mana': ['Int = 1.20', 'dsadas', 'whatever', '-_-'], 'Skill': 'true', 'Magic': 'true'}}


header = ['Name', 'Skill', 'Magic', 'wo_mana', 'wi_mana', 'mana']

with open('output.csv', 'w') as f:
w = csv.DictWriter(f, header)
w.writeheader()
for key, val in sorted(d.items()):
row = {'Name': key}
# print val
row.update(val)
dictionary_containing_list = {key: val[key] for key in val if type(val[key])==list}
if dictionary_containing_list:
max_length = len(max(dictionary_containing_list.iteritems(), key=lambda x: len(x[1]))[1])
dict_list = []
for i in range(max_length):
dict_ = {}
for key, val_list in dictionary_containing_list.iteritems():
try:
dict_[key] = val_list[i]
except IndexError:
pass
dict_list.append(dict_)
row.update(dict_list[0])
print dict_list
w.writerow(row)
if dictionary_containing_list:
[w.writerow(dict_) for dict_ in dict_list[1:]]

该解决方案非常通用,您可以添加任意数量的mana/列,它就会起作用。请记住将准确的列名称附加到 header 列表中!

关于Python:如何将嵌套字典打印为具有可变行的复杂表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38115533/

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