7

I'm writing a simple script that is trying to extract the first element from the second column of a .txt input file.

import sys

if (len(sys.argv) > 1):
    f = open(sys.argv[1], "r");
    print "file opened";

line = [];

for line in f:
    line = line.strip("\n ' '")
    line = line.split(",") 
    print line[1]

f.close();

My input file looks like this:

Client 192.168.1.13 said ``ACC: d0bb38f18da536aff7b455264eba2f1e35dd976f,389182.567,-0.042,-0.893,0.333''
Client 192.168.1.13 said ``ACC: d0bb38f18da536aff7b455264eba2f1e35dd976f,389182.590,-0.036,-0.905,0.273''
Client 192.168.1.13 said ``ACC: d0bb38f18da536aff7b455264eba2f1e35dd976f,389182.611,-0.046,-0.948,0.204''
Client 192.168.1.13 said ``ACC: d0bb38f18da536aff7b455264eba2f1e35dd976f,389182.631,-0.074,-0.978,0.170''
Client 192.168.1.13 said ``ACC: d0bb38f18da536aff7b455264eba2f1e35dd976f,389182.654,-0.100,-1.006,0.171''

I want my delimiter to be a comma. When I print the length of the line out, I'm getting 5 elements (as expected). However, whenever I try to index the list to extract the data (i.e., when I call print line[1]), I keep getting the following error:

file opened
Traceback (most recent call last):
  File "stats.py", line 13, in <module>
    print line[1]
IndexError: list index out of range

I don't understand why it's out of range when clearly it isn't.

4

4 回答 4

9

我猜你的文件中某处有一个空行。如果它遍历数据然后生成异常,则空行将位于文件的末尾。

请插入

print len(line), line

在你之前

print line[1]

作为检查以验证是否是这种情况。

您始终可以使用此构造来测试空行,并且只处理/打印空行:

for line in f:
    line = line.strip()
    if line:
       # process/print line further
于 2012-06-26T23:33:10.503 回答
2

当您使用列表并尝试在特定索引处获取值时,始终可以安全地看到索引在范围内

if len(list_of_elements) > index: 
   print list_of_elements[index]

看:

>>> list_of_elements = [1, 2, 3, 4]
>>> len(list_of_elements)
4
>>> list_of_elements[1]
2
>>> list_of_elements[4]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> 

现在你必须找出为什么你的列表没有包含你预期的那么多元素

解决方案:

import sys

if (len(sys.argv) > 1):
    f = open(sys.argv[1], "r")
    print "file opened"

for line in f:
    line = line.strip().strip('\n')
    # Ensure that you are not working on empty line
    if line:
        data = line.split(",") 
    # Ensure that index is not out of range
    if len(data) > 1: print data[1]

f.close()
于 2012-06-26T23:33:15.550 回答
2

您的数据后可能有空行,我在没有它们的情况下运行了您的测试代码,它按预期工作。

$ python t.py t.txt
file opened
389182.567
389182.590
389182.611
389182.631
389182.654

如果您不想删除它们,则只需检查空行。

for line in f:
    if line.strip(): # strip will remove all leading and trailing whitespace such as '\n' or ' ' by default    
        line = line.strip("\n ' '")
        line = line.split(",") 
        print line[1]
于 2012-06-26T23:37:47.063 回答
0

捕获异常并打印违规行可能很有用

for line in f:
    line = line.strip("\n ' '")
    line = line.split(",") 
    try:
        print line[1]
    except IndexError, e:
        print e
        print "line =", line
        raise   # if you don't wish to continue
于 2012-06-27T00:26:00.847 回答