【发布时间】:2014-03-25 14:24:37
【问题描述】:
我正在尝试保存像这样的字典:
Allfiles={'woof': '0', 'jpg': '0', 'js': '45', 'gif': '0', 'css': '11', 'png': '6'}
作为 Excel 文件。
我的代码是这样的:
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')
当我检查 Excel 时,输出是嘲笑。抱歉,我的声誉不足以发布图片,但问题是程序将整数 '11' 和 '45' 视为字符串并将它们放在不同的单元格中。但如果我没有将这些值设置为字符串,则会出现错误提示“int is not iterable”。那么有人可以帮帮我吗?
更新: 像这样的新字典:
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': []}
当我使用代码时:
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')
顺便说一句,新字典应该保存在 Excel 文件中的其他 2 下。
我有一个回溯说:
sheet1.write(row+1,col,item)
Exception: Attempt to overwrite cell: sheetname=u'Parsed data' rowx=1 colx=0
有人知道吗?
【问题讨论】:
-
您期望什么输出格式?键是行还是列?
-
“但如果我没有将这些值设置为字符串”是的,你做到了:
'css': '11'。你是如何创建你的字典的?你什么时候有"int is not iterable"?
标签: python dictionary xlwt