2016-11-15 1 views
0

이 함수는 매개 변수가 공백으로 남겨지면 모든 상태를 특정 상태 또는 모든 상태로 채 웁니다. 매개 변수를 공백으로 남겨 두었을 때 파일의 처음 3 행에 헤더가 있기 때문에 오류가 발생합니다.헤더가없는 CVS 파일의 목록을 만듭니다.

def findpop(state=None): 
    f=open(getMediaPath("population_state_reduced (2).csv"),"rt") 
    for line in f: 
     parts = line.split(',') 
     if state is None: 
      return [(parts[4], int(parts[5]))] 
     else: 
     for line in f: 
      if parts[4] == state.capitalize(): 
        return int(parts[5]) 
print findpop() 
+1

은 처음 3 개 라인을 통해 건너 뛰기 :'에 대한이 _ 범위 (3) 다음 (F)를' – AChampion

답변

1

그냥 건너 뛰 처음 3 개 라인 :

def findpop(state=None): 
    f = open(getMediaPath("population_state_reduced (2).csv"), "rt") 
    index = 1 
    for line in f: 
     if index > 3: 
      parts = line.split(',') 
      if state is None: 
       return [(parts[4], int(parts[5]))] 
      else: 
       for line in f: 
        if parts[4] == state.capitalize(): 
         return int(parts[5]) 
     index += 1 

print findpop() 
+0

지금은 대신 3 행을 반환 전체 란 –

관련 문제