Python:如何将字典中的值写入文件?

2025-01-05 11:07:27
推荐回答(2个)
回答1:

yourDict={'1000':{'1':['a','b','c','d'],'2':['e','b','c','a']},'2000':{'1':['c','d','c','d'],'2':['a','a','c','d']}}
out=open('out.xls','w')
for key in yourDict:
out.write(key)
for key2 in yourDict[key]:
out.write('\t')
out.write(key2+'\t')
out.write('\t'.join(yourDict[key][key2] ))
out.write('\n')

最后xls转存为csv即可

回答2:

写入文件就是自己裁解这个字典了啊。发现你这是字典套字典再套列表。分开字典用key,然后再key,最终将解读出来的值write进文件。