2014-03-27 2 views
0

여기 여러 개의 사전에 읽고 저장해야하는 Excel 파일이 있으며 그 중 하나는 목록과 함께 있습니다. 다른 2 잘 작동하지만 '목록'하나는 아무것도 저장합니다. 다음과 같은 Excel 파일 모양입니다 : 목록과 사전 5 선 afterwords에서 저장for 루프를 사용하여 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/  

, 그것은으로 초기화된다

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)) 

그러나 출력은 사전에 아무 것도 저장하지 않고 초기화 된 그대로 공백 사전 만 제공합니다. 누구나 코드를 개선 할 생각이 있습니까?

+0

LEN (DAlllists [ 'dstip는'])는 0이 있으므로 루프가 전혀 실행되지 않습니다이다. 빈 셀을 얻을 때까지 반복해야합니다. 어쩌면 나중에 대답을 쓸 것입니다. – x3al

답변

0

이 모든 코드를 제거하고 사용해보십시오 :

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:] 
관련 문제