简体   繁体   English

Python抛出IndexError:列表索引超出范围,使用CSV

[英]Python throwing IndexError: list index out of range, with CSV

I have had an error in a Python program when trying to read a CSV file. 尝试读取CSV文件时,Python程序出现错误。 What is most perplexing is the error was not there initially, and has "come and gone" once before, but has now returned. 最令人困惑的是,该错误最初并不存在,并且曾经有过“来来往往”的错误,但现在又回来了。 The program is simple and the source CSV file has not been altered at any time. 该程序很简单,并且源CSV文件没有随时更改。 Here is the relevant snippet... 这是相关的片段...

  with open('/home/rob/Applications/Ambient/timings.csv', 'rt') as csvfile:
  fr = csv.reader(csvfile, delimiter=',', quotechar='|')
  for row in fr:
    previous=delay
    delay=0
    millis = int(row[1])

The CSV file has this structure: CSV文件具有以下结构:

0,824 
1,496 
0,356 
1,792 
0,388 
0,764 
1,560 
0,1264 
1,724 
0,2820 
1,496 
for ~88k lines

This is the error that is thrown: 这是引发的错误:

Traceback (most recent call last): File "/home/rob/Applications/Ambient/AmbientDecode_02.py", line 82, in millis = int(row[1]) IndexError: list index out of range 追溯(最近一次呼叫最近):文件“ /home/rob/Applications/Ambient/AmbientDecode_02.py”,第82行,单位为millis = int(row [1])IndexError:列表索引超出范围

As you can see the accessing of the row[1] occurs early in the program after setting up the access to the CSV, and initially it did not cause an error. 如您所见,在设置对CSV的访问之后,对行[1]的访问发生在程序的早期,最初并没有引起错误。 When the error arose later on, I definitely had not altered either that part of the Python code, or the CSV file. 稍后出现错误时,我绝对没有更改Python代码的那部分或CSV文件。 To pass the time I began work on some code further on, and suddenly the program began to run again. 为了消磨时间,我开始进一步编写一些代码,然后突然程序再次开始运行。 Now it has stopped again, and the same index error has returned. 现在它再次停止,并且返回了相同的索引错误。 If I add print row after the millis = int(row[1]) the program runs??? 如果我在millis = int(row [1])之后添加打印行 ,程序将运行???

I need a definitive answer, as I can't go on just fiddling with subsequent code and hoping somehow I fix code that I have no idea why it has crashed??? 我需要一个明确的答案,因为我无法继续摆弄后续代码,并希望以某种方式修复不知道为什么崩溃的代码???

Any ideas? 有任何想法吗?

Rob

What the error means is that, in one of the subsequent ~88k lines there is no comma and second data following the comma. 错误的意思是,在随后的〜88k行之一中,没有逗号,并且在逗号之后没有第二个数据。 You can modify the program to handle the error gracefully, thus: 您可以修改程序以正常处理错误,因此:

with open('/home/rob/Applications/Ambient/timings.csv', 'rt') as csvfile:
    fr = csv.reader(csvfile, delimiter=',', quotechar='|')
        for row in fr:
            previous=delay
            delay=0
            try:
                millis = int(row[1])
            except IndexError:
                pass

The above code would just ignore the line where it can not find the second data (following the comma) and continues to the next line. 上面的代码将忽略无法找到第二个数据的行(逗号后面),并继续到下一行。 You could instead define what needs to be done in such a scenario, by replacing pass in the above code with that logic. 您可以pass将上述代码中的pass替换为该逻辑来定义在这种情况下需要执行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM