简体   繁体   English

如何将字典另存为Excel文件

[英]how to save dictionary as excel file

I am trying to save a dictionary like : 我正在尝试保存一个字典,如:

Allfiles={'woof': '0', 'jpg': '0', 'js': '45', 'gif': '0', 'css': '11', 'png': '6'} 

as an Excel file. 作为Excel文件。

And my code is like: 我的代码就像:

workbook=xlwt.Workbook(encoding='ascii')
sheet1=workbook.add_sheet('Parsed data')
for col, caption in enumerate(Allfiles):
    sheet1.write(0,col,caption)
    for row,item in enumerate(Allfiles[caption]):
        sheet1.write(row+1,col,str(item))
workbook.save('savexl.xls')

When I check the Excel the output is ridicules. 当我检查Excel时,输出为嘲笑。 Sorry my reputation is not enough to post a picture, but the problem is that the program treats integer '11' and '45' as string and put them in different cell. 抱歉,我的声誉不足以发布图片,但是问题是该程序将整数'11'和'45'视为字符串并将它们放在不同的单元格中。 But if I didn't set those values to string, an error said "int is not iterable". 但是,如果我没有将这些值设置为字符串,则会出现一个错误,提示“ int不可迭代”。 So any one can help me out? 那么有人可以帮我吗?


Update: a new dictionary like: 更新:像这样的新词典:

Alllists={'scrip': ['10.183.195.140'], 'host': ['edigitalsurvey.com', 'ichef.bbci.co.uk',        'notify3.dropbox.com', 'sa.bbc.co.uk', 'static.bbci.co.uk', 'www.bbc.co.uk'], 'dstip': ['108.160.162.38', '212.58.244.69', '46.236.9.36', '77.72.112.168', '81.23.53.170', '81.23.53.171'], 'referer':    ['http://static.bbci.co.uk/frameworks/barlesque/2.60.6/orb/4/style/orb-fixed.css', 'http://static.bbci.co.uk/h4discoveryzone/0.233.1/style/h4discoveryzone.css', 'http://static.bbci.co.uk/h4drawers/0.66.1/style/h4drawers.css', 'http://static.bbci.co.uk/h4more/0.114.2/style/h4more.css', 'http://static.bbci.co.uk/h4popular/0.130.1/style/h4popular.css', 'http://static.bbci.co.uk/h4whatson/0.176.5/style/h4whatson.css', 'http://www.bbc.co.uk/'], 'server': []}

When I use code: 当我使用代码时:

for col,caption in enumerate(Alllists):
    sheet1.write(4,col,caption)
    for row,item in enumerate(Alllists[caption]):
        sheet1.write(row+1,col,item)
workbook.save('savexl.xls')

btw, the new dictionary should be saved under the other 2 in the Excel file. 顺便说一句,新字典应保存在Excel文件中的其他2下。

I got a trace back said: 我回想起说:

sheet1.write(row+1,col,item)
Exception: Attempt to overwrite cell: sheetname=u'Parsed data' rowx=1 colx=0

Anyone got idea? 有人知道吗?

Your dictionary has only one row instead of list of rows. 您的字典只有一行而不是行列表。 You can change your dictionary structure like that: 您可以像这样更改字典结构:

Allfiles={'woof': [0], 'jpg': [0], 'js': [45], 'gif': [0], 'css': [11], 'png': [6]} 

or change your loop to something like: 或将循环更改为:

workbook=xlwt.Workbook(encoding='ascii')
sheet1=workbook.add_sheet('Parsed data')
for col, caption in enumerate(Allfiles):
    sheet1.write(0,col,caption)
    sheet1.write(1,col,Allfiles[caption])
workbook.save('savexl.xls')

to work with original dictionary. 使用原始字典。

The only reason your code does not throw exception is because string in python counts as a list of characters so you can use '11' as ['1', '1']. 您的代码不会引发异常的唯一原因是因为python中的字符串被视为字符列表,因此您可以将'11'用作['1','1']。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM