[英]How to use for loop to save an Excel file into a dictionary?
这是一个Excel文件,应阅读并保存在多个词典中,其中1个带有列表。 其他2个效果很好,但该“列表”没有节省任何费用。 这是Excel文件的样子:
woof jpg js gif css png
0 0 45 0 11 6
total_time ip_packets_num http_packets_num avg_http_size packet_num tcp_packets_num
76.11243916 395 200 378 1217 395
srcip host dstip referer server
10.183.195.140 edigitalsurvey.com 108.160.162.38 http://static.bbci.co.uk.css
ichef.bbci.co.uk 212.58.244.69 http://static.bbci.co.uk.css
notify3.dropbox.com 46.236.9.36 http://static.bbci.co.uk.css
sa.bbc.co.uk 77.72.112.168 http://static.bbci.co.uk/.css
static.bbci.co.uk 81.23.53.170 http://static.bbci.co.uk.css
www.bbc.co.uk 81.23.53.171 http://static.bbci.co.uk.css
http://www.bbc.co.uk/
带列表的字典从5行后缀中保存,其初始化为:
DAlllists={'scrip':[],'dstip':[],'host':[],'referer':[],'server':[]}
我正在使用的代码是:
for caption in range(len(DAlllists)):
if Dworksheet.cell_value(4,caption)=='srcip':
for row in range(len(DAlllists['srcip'])):
DAlllists['srcip'].append(Dworksheet.cell_value(5+row,caption))
if Dworksheet.cell_value(4,caption)=='dstip':
for row in range(len(DAlllists['dstip'])):
DAlllists['dstip'].append(Dworksheet.cell_value(5+row,caption))
if Dworksheet.cell_value(4,caption)=='host':
for row in range(len(DAlllists['host'])):
DAlllists['host'].append(Dworksheet.cell_value(5+row,caption))
if Dworksheet.cell_value(4,caption)=='referer':
for row in range(len(DAlllists['referer'])):
DAlllists['referer'].append(Dworksheet.cell_value(5+row,caption))
if Dworksheet.cell_value(4,caption)=='server':
for row in range(len(DAlllists['server'])):
DAlllists['server'].append(Dworksheet.cell_value(5+row,caption))
但是输出似乎没有将任何内容保存到该字典中,而只是在初始化时提供空白字典。 任何人都有改善代码的想法吗?
尝试删除所有代码并使用此代码:
def read_xc_column(sheet, column, startrow=0):
row = startrow
value = sheet.cell_value(row, column)
answer = []
while value:
answer.append(value)
row = row + 1
value = sheet.cell_value(row, column)
return answer
DAlllists = {}
for x in range(5):
this_col = read_xc_column(Dworksheet, x, 4)
DAlllists[this_col[0]] = this_col[1:]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.