在excel文件中编写字典值 [英] Write dictionary values in an excel file
本文介绍了在excel文件中编写字典值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
d.setdefault(key,[])。append(values)
关键值对应如下所示:
a -el1,el2,el3
b -el1,el2
c -el1
我需要通过字典循环并写入一个excel文件:
列1列2
pre>
a el1
el2
el3
b el1
el2
c el1
对于在excel文件中编写,我使用xlsxwriter。我需要帮助循环单独通过字典,因为在写入密钥后,我不需要再写入,直到我完成所有相应的值。
解决方案似乎你想要这样的东西:
import xlsxwriter
workbook = xlsxwriter.Workbook('data.xlsx')
工作表= workbook.add_worksheet()
d = {'a':['e1','e2','e3'] ,'b':['e1','e2'],'c':['e1']}
row = 0
col = 0
d.keys():
row + = 1
worksheet.write(row,col,key)
在d [key]中的项目:
worksheet.write(row, col + 1,item)
row + = 1
workbook.close()
I have a dictionary with multiple values for each key. I add the values using the following code:
d.setdefault(key, []).append(values)
The key value correspondence looks like this:
a -el1,el2,el3 b -el1,el2 c -el1
I need to loop thru the dictionary and write in an excel file:
Column 1 Column 2 a el1 el2 el3 b el1 el2 c el1
For writing in the excel file I use xlsxwriter. I need help looping separately thru the dictionary, because after writing the key and I don't need to write it again until I finish all the corresponding values.
解决方案It seems like you want something like this:
import xlsxwriter workbook = xlsxwriter.Workbook('data.xlsx') worksheet = workbook.add_worksheet() d = {'a':['e1','e2','e3'], 'b':['e1','e2'], 'c':['e1']} row = 0 col = 0 for key in d.keys(): row += 1 worksheet.write(row, col, key) for item in d[key]: worksheet.write(row, col + 1, item) row += 1 workbook.close()
这篇关于在excel文件中编写字典值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文