我试图用随机生成的字符串和数字编写一个字典到Excel文件。我几乎成功了,但有一个小问题。我的字典的结构如下:如何将python字典写入excel文件?
Age: 11, Names Count: 3, Names: nizbh,xyovj,clier
本字典是从通过文本文件获取的数据生成的。它根据年龄汇总所有内容,如果两个人年龄相同,则将它们组合成一个列表。我试图将这些数据写入Excel文件。到目前为止,我已经写了这段代码。
import xlsxwriter
lines = []
workbook = xlsxwriter.Workbook('demo.xlsx')
worksheet = workbook.add_worksheet()
with open ("input2.txt") as input_fd:
lines = input_fd.readlines()
age_and_names = {}
for line in lines:
name,age = line.strip().split(",")
if not age in age_and_names:
age_and_names[age]=[]
age_and_names[age].append(name)
print age_and_names
for key in sorted(age_and_names):
print "Age: {}, Names Count: {}, Names: {}".format(key, len(age_and_names[key]), ",".join(age_and_names[key]))
row=0
col=0</