不超出范围时列出索引超出范围错误

时间:2021-08-05 16:23:07

I have a data file which only contains the following line:

我有一个只包含以下行的数据文件:

"testA" "testB":1:"testC":2

Now when I split this line, and print the resulting list(w) I get the following:

现在,当我分割这一行,并打印结果列表(w)时,我得到以下内容:

['"testA"', '"testB":1:"testC":2']

[]

Now when I want to access w[0], it returns "testA" just fine, but when I print w[1], it crashes and gives a list index out of range error, but still prints it out "testB":1:"testC":2

现在,当我想访问w [0]时,它返回“testA”就好了,但是当我打印w [1]时,它会崩溃并给出一个超出范围错误的列表索引,但仍会将其打印出“testB”:1 : “TESTC”:2

Any help would be much appreciated!

任何帮助将非常感激!

4 个解决方案

#1


1  

Your code does not crash on w[1] on the "testA" "testB":1:"testC":2 line, otherwise it would not print "testB":1:"testC":2. Note the additional [] in your output? Your file contains some more empty lines, which are split to [], and which will then produce that error even on w[0].

你的代码在“testA”“testB”上的w [1]上不会崩溃:1:“testC”:2行,否则它不会打印“testB”:1:“testC”:2。注意输出中的附加[]?您的文件包含一些空行,它们被拆分为[],然后即使在w [0]上也会产生该错误。

To fix the problem, you should check whether the line and/or the list created from that line is non-empty. (When testing the line, make sure to strip away any whitespace, such as the trailing newline character.) Your code should then look somewhat like this:

要解决此问题,您应检查从该行创建的行和/或列表是否为非空。 (在测试行时,请务必去除任何空格,例如尾部换行符。)您的代码应该看起来像这样:

with open("test.txt") as f:
    for line in f:
        if line.strip():  # strip the `\n` before checking whether line is empty
            w = line.split()
            print(w[0], w[1])

#2


0  

working for me without any error:

为我工作没有任何错误:

>>> a = '"testA" "testB":1:"testC":2'
>>> b = a.split(' ')
>>> b
['"testA"', '"testB":1:"testC":2']
>>> b[0]
'"testA"'
>>> b[1]
'"testB":1:"testC":2'

ae you doing anything different?

你做了什么不同的事情吗?

#3


0  

I also cannot reproduce your error but want to share a tip that I use in situations where I'm reading from a file and splitting on a delimiter.

我也无法重现您的错误,但想要分享我在从文件读取并拆分分隔符的情况下使用的提示。

cleaned_list = [ a.strip() for a in input_line.split(' ') if a ] 

Simple and sanitizes the split list well.

简单并且很好地清理拆分列表。

#4


0  

The answer was that the program read the next line after the first one which it split, and that line was empty. So as soon as it tried to index the content of that (empty line) it crashed. That's why it DID print the first line, and then crashed. Thanks a lot for your help, though!

答案是程序在分割的第一行之后读取下一行,该行为空。因此,一旦它试图索引该内容(空行),它就会崩溃。这就是为什么DID打印第一行然后崩溃的原因。非常感谢你的帮助!

#1


1  

Your code does not crash on w[1] on the "testA" "testB":1:"testC":2 line, otherwise it would not print "testB":1:"testC":2. Note the additional [] in your output? Your file contains some more empty lines, which are split to [], and which will then produce that error even on w[0].

你的代码在“testA”“testB”上的w [1]上不会崩溃:1:“testC”:2行,否则它不会打印“testB”:1:“testC”:2。注意输出中的附加[]?您的文件包含一些空行,它们被拆分为[],然后即使在w [0]上也会产生该错误。

To fix the problem, you should check whether the line and/or the list created from that line is non-empty. (When testing the line, make sure to strip away any whitespace, such as the trailing newline character.) Your code should then look somewhat like this:

要解决此问题,您应检查从该行创建的行和/或列表是否为非空。 (在测试行时,请务必去除任何空格,例如尾部换行符。)您的代码应该看起来像这样:

with open("test.txt") as f:
    for line in f:
        if line.strip():  # strip the `\n` before checking whether line is empty
            w = line.split()
            print(w[0], w[1])

#2


0  

working for me without any error:

为我工作没有任何错误:

>>> a = '"testA" "testB":1:"testC":2'
>>> b = a.split(' ')
>>> b
['"testA"', '"testB":1:"testC":2']
>>> b[0]
'"testA"'
>>> b[1]
'"testB":1:"testC":2'

ae you doing anything different?

你做了什么不同的事情吗?

#3


0  

I also cannot reproduce your error but want to share a tip that I use in situations where I'm reading from a file and splitting on a delimiter.

我也无法重现您的错误,但想要分享我在从文件读取并拆分分隔符的情况下使用的提示。

cleaned_list = [ a.strip() for a in input_line.split(' ') if a ] 

Simple and sanitizes the split list well.

简单并且很好地清理拆分列表。

#4


0  

The answer was that the program read the next line after the first one which it split, and that line was empty. So as soon as it tried to index the content of that (empty line) it crashed. That's why it DID print the first line, and then crashed. Thanks a lot for your help, though!

答案是程序在分割的第一行之后读取下一行,该行为空。因此,一旦它试图索引该内容(空行),它就会崩溃。这就是为什么DID打印第一行然后崩溃的原因。非常感谢你的帮助!