【发布时间】:2017-11-19 18:22:38
【问题描述】:
我需要扫描 .csv 文件中的每一行,但一旦我扫描到最后,就会出现错误。
def percentage():
for line in csv_file:
temp = line.strip("\n")
measureType = temp.split(",")[5][1: -1]
if measureType == "PERCENT":
year = line.split(",")[1][1: -1]
percentage = line.split(",")[6][1: -2]
country = line.split(",")[0][1: -1]
if float(percentage) < 50:
output.addCountry(country, year, percentage)
当我扫描文件时,一旦它到达末尾,它就会给我一个错误:
IndexError: list index out of range
对于下面的行:
measureType = temp.split(",")[5][1: -1]
我非常困惑,不知道我必须解决什么问题。
【问题讨论】:
-
文件的最后一行是什么?
-
听起来这个文件最后有一个空行。
标签: python python-2.7 file csv