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.