该函数返回单个状态和种群,但我需要返回列中的所有种群,但跳过前6行
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 :(得分:1)
如果我理解你的话,这正是你想要的:
import csv
def findpop(state=None):
res = []
with open(getMediaPath("population_state_reduced (2).csv")) as f:
reader = csv.reader(f)
for i, line in enumerate(reader):
if i > 5: # skip first 6 rows
if state is None:
res.append((line[4], int(line[5])))
else:
if line[4] == state.capitalize():
return int(line[5])
return res