简体   繁体   中英

Having trouble resolving a 'list index out of range' error

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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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