制作没有标题的CVS文件列表

时间:2016-11-15 13:33:16

标签: python jython

当参数留空时,此函数会查找给定特定状态或所有状态的总体。当我留下参数空白虽然它给我一个错误,因为文件的前3行中有标题。

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 个答案:

答案 0 :(得分: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()