0

I am having trouble resolving a 'list index out of range' error. The code seems to operate fine until a certain point when file_no2 = line2.split()[0] generates the index error. The error shows up 70 lines before the end of the file that is being read, so I cannot figure why the 'list index out of range error' occurs. I am attempting to iterate through the file until line2 is populated by the last line of data in the file, so I used the range function with a previously calculated sum of the number of lines in the file (l). I subtracted 1 in the range calculation with the intent to stop the loop once line1 is populated by the second to last line in the file. But again, the index error is stopping the process 70 lines short of the end of the file, so I do not understand why it is out of range.

for i in range(l-1):
    line1 = trackdata.readline()
    line2 = trackdata.readline()

    file_no1 = line1.split()[0]
    time1 = line1.split()[1]
    x1 = line1.split()[2]
    y1 = line1.split()[3]
    length1 = line1.split()[4]
    flow_dir1 = float(line1.split()[5])
    flow_mag1 = line1.split()[6]

    file_no2 = line2.split()[0]
    time2 = line2.split()[1]
    x2 = line2.split()[2]
    y2 = line2.split()[3]
    length2 = line2.split()[4]
    flow_dir2 = float(line2.split()[5])
    flow_mag2 = line2.split()[6]

    if file_no1 == file_no2:
        if abs(flow_dir2 - flow_dir1) > 90.0:
            print x1, y1
            #print >> coordinates2, x2, y2

Here is an example of the results:

185313.5426 112700.3316

1091 153.4636750 184498.3329 112815.9754 100.0000000 344.7592449 0.6516200005

184500.6344 112716.0019

1091 649.7940156 184461.4951 113012.3586 300.0000000 353.5487391 0.3463617710

1091 1599.736768 184398.7140 113126.0630 440.6196278 341.8759486 0.1121731124

1091 1734.946452 184382.9241 113119.2729 457.9156941 349.0664262 0.1303822198

...

1123 0.0000000000 184110.8309 113518.9487 0.0000000000 271.3035311 0.1646996924

And the error message:

Traceback (most recent call last): File "O:\ArcGIS\courseypond\particletrack\check_trackangle_5yr", line 28, in > file_no2 = line2.split()[0] IndexError: list index out of range

The input data is read from a space-delimited text file that contains 14910 lines. Each line of data is formatted as in the example results above: file number, time, x-coordinate, y-coordinate, length, flow direction, flow magnitude. The goal is to identify and store the coordinate points when two lines with the same file number identifier show a flow direction change greater than 90 degrees.

  • 1
    Can you post the exact Error and some example imput? If possible posting a minimal example (so that we can reproduce the error) will help enormously. Could it be that the file is simply bad? Maybe there are just 3 items in one line so that `line.split()[4]` fails. – syntonym Feb 25 '15 at 19:03
  • 1
    print out `line2` just before the error – Gillespie Feb 25 '15 at 19:03
  • 1
    If error is at `file_no2 = line2.split()[0]` then I guess line2 is empty. – Tuan Anh Hoang-Vu Feb 25 '15 at 19:06
  • @syntonym: I updated the discussion to include text of the error message and an example of the output result. The input file contains 14910 lines of data, and the error occurs 70 lines short of the end of the file, so I'm not sure how that is reproducible with only a minimal sample of the input data. The code that generates the error calls for the first item of the line, which is always present through the last line in the file. – rockhoundmatt Feb 25 '15 at 19:43
  • @RPGillespie If I add the print line2 command, it is clear that the loop does cycle through the file until the second to last line (see the example results added to the question discussion). The final line2 that prints is the second to last line in the file, which I guess means that there will be no line2 to match with line1 for the last line of the file. This makes me think that I need to add a statement at the end of the loop that equates a new line1 to the existing line2. And maybe I need to set the initial line1 and line2 outside the loop? – rockhoundmatt Feb 25 '15 at 19:55
  • @rockhoundmatt You are only comparing "adjacent lines". Is that by design? Should the following input trigger your algorithm: `1 0 0 0 0 1 0\n 1 0 0 0 0 60 0\n1 0 0 0 0 120 0` (sorry for the bad formatting)? – syntonym Feb 25 '15 at 20:03
  • @syntonym Yes. Adjacent lines that share the same file number identifier. I only want to find and store the coordinate points from lines that have the same file number identifier and show a change in the flow direction angle that is greater than 90 degrees. UPDATE... I think I have now resolved the issue. Posting my solution. Thanks. – rockhoundmatt Feb 25 '15 at 20:20

1 Answers1

0

Thank you for the suggestions. It appears that I managed to resolve the issue by relocating the initial line1 and line2 assignments outside of the loop and reassigning line1 to equal line2 at the end of the loop. No more 'list index out of range error' and each line of data from the file appears to get paired for comparison. The updated code is as follows:

line1 = trackdata.readline()
line2 = trackdata.readline()

for i in range(l-1):

    file_no1 = line1.split()[0]
    time1 = line1.split()[1]
    x1 = line1.split()[2]
    y1 = line1.split()[3]
    length1 = line1.split()[4]
    flow_dir1 = float(line1.split()[5])
    flow_mag1 = line1.split()[6]

    file_no2 = line2.split()[0]
    time2 = line2.split()[1]
    x2 = line2.split()[2]
    y2 = line2.split()[3]
    length2 = line2.split()[4]
    flow_dir2 = float(line2.split()[5])
    flow_mag2 = line2.split()[6]

    if file_no1 == file_no2:
        if abs(flow_dir2 - flow_dir1) > 90.0:
            print x1, y1
            #print >> coordinates2, x2, y2

    line1 = line2
    line2 = trackdata.readline()
  • Great you could resolve your problem! Here are a few pointers to make your solution "more pythonic" if you want to: You could do all the assignments in one line, like in [this](https://stackoverflow.com/questions/24999875/python-assignment-destructuring) question. It would look like the following: `file_no1, time1, x1, y1, length1, flow_dir1, flow_mag1 = line1.split()` although this does not deal with converting `flow_dir1` to float. [This](https://stackoverflow.com/questions/2631189/python-every-other-element-idiom) could replace your for loop. Running out of chars, if want to know more, ask. – syntonym Feb 25 '15 at 20:34
  • @syntonym Thanks. Streamlining the code is not very important, as this is just a means to an end. I suppose I could convert flow_dir to float in the absolute value calculation. As far as I know, though, I still would need to iterate through the file line by line to compare the data. I only know how to do that with the loop structure. For now, I have what I need, however. – rockhoundmatt Feb 25 '15 at 20:43