[英]csv, IndexError: list index out of range
I am reading and doing examples from the book Python Crash Course . 我正在阅读Python速成课程并在其中做示例。 Now I get stuck in reading data from a csv file. 现在,我陷入了从csv文件读取数据的困境。 I'm trying to get values of max_temperatures
and read it, but when I did this in the same way as in my book then an error is shown: 我试图获取max_temperatures
值并读取它,但是当我以与本书相同的方式进行此操作时,将显示错误:
IndexError:
high = int(row[1])
IndexError: list index out of range
Code: 码:
import csv
filename = "sitka_weather_history_2014.csv"
with open(filename) as f:
reader = csv.reader(f)
header_row = next(reader)
highs = []
for row in reader:
high = int(row[1])
highs.append(high)
print(highs)
is high
the first element in the row? high
是该行的第一个元素? If so, remember that Python starts counting from 0 - so it should be high = int(row[0])
如果是这样,请记住Python从0开始计数-因此它应该为high = int(row[0])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.