2017-12-07 9 views
1

내가 다음 코드는 라인 (20)에이 오류가 발생지도 감소 -리스트 인덱스 범위를 벗어지도 매퍼에

#!/usr/bin/env python 
import sys 

myList = [] 
n = 10 # Number of top N records 

for line in sys.stdin: 
    # remove leading and trailing whitespace 
    line = line.strip() 
    # split data values into list 
    data = line.split(";") 

    # convert weight (currently a string) to int 
    try: 
     balance = int(data[6]) 
    except ValueError: 
     # ignore/discard this line 
     continue 

    # add (weight, record) touple to list 
    myList.append((balance, line)) 
    # sort list in reverse order 
    myList.sort(reverse=True) 

    # keep only first N records 
    if len(myList) > n: 
     myList = myList[:n] 

# Print top N records 
for (k,v) in myList: 
    print(v) 

mapperwith지도를 가지고 :

balance = int(data[6]) 

IndexError: list index out of range

프로세스가 존재하지 않는 파이프를 오른쪽으로 보려고했습니다.

age job marital education default balance housing loan contact day month duration campaign pdays previous poutcome y 
30 unemployed married primary no 1787 no no cellular 19 oct 79 1 -1 0 unknown no 
33 services married secondary no 4789 yes yes cellular 11 may 220 1 339 4 failure no 

어떤 아이디어 : 여기

은 데이터 세트의 샘플입니다?

+0

데이터가 공백으로 구분 된 경우 split ("\\ s +") 대신 –

+0

@RAZ_Muh_Taz를 호출합니다. 여러 가지 다른 분할 및 동일한 결과를 시도했습니다. 내 코드에 또 다른 문제가 있습니까? –

답변

0

바로 몇 가지 문제가 있습니다. 샘플 데이터는 탭 구분 된 것으로 보이지만 ";" 대신 "\ t"을 시도하십시오. 또한 여섯 번째 필드는 균형이 아닙니다. 주택입니다. 사용 필드 5입니다.

이와 같은 많은 작업을 수행하려면 python의 csv 모듈을 살펴보십시오.

+0

여전히 같은 오류가 발생합니다. Hahah 악몽은 그것을 쪼개서 거기에있는 것을보고 나서 –

+0

인쇄 데이터입니다. –

관련 문제