简体   繁体   中英

Why am I getting an IndexError from a for loop?

I'm writing code that will take dates and numeric values from a csv file and compare them.

date_location = 3
numeric_location = 4

with open('file1.csv', 'r') as f1:
    next(f1)
    with open('file2.csv', 'r') as f2:
        next(f2)
        for i in (f1):
            f1_date = (i.split()[date_location])
            f1_number = (i.split()[numeric_location])
            for j in (f2):
                f2_date = (j.split()[date_location])
                f2_number = (j.split()[numeric_location])
                print(f1_date, f1_number)
                print(f2_date, f2_number)
                if f1_date == f2_date:
                    print(f1_date == f2_date)
                    if f2_number > f1_number:
                        print('WIN')
                        continue
                    elif f2_number <= f1_number:
                        print('lose')
               f2.seek(0, 0)`

I get this error IndexError: list index out of range for f1_date = (i.split()[date_location]) , which i assume will also affect:

f1_number = (i.split()[numeric_location])

f2_date = (j.split()[date_location])

f2_number = (j.split()[numeric_location])

Can anyone explain why? I haven't found a way to make it so this error doesn't show.

EDIT: I forgot to change the separator for .split() after messing around with the for loop using text files

Two main possibilities:

1) Your csv files are not space delimited, and as the default separator for .split() is " " , you will not have at least 4 space-separated items in i.split() (or 5 for numeric_location ).

2) Your csv is space delimited, but is ragged, ie it has incomplete rows, so for some row, there is no data for column 4.

I also highly suggest using a library for reading csvs. csv is in the standard library, and pandas has built-in handling of ragged lines.

The error is happening because you only have one element in the case of i.split() But date_location is equal to 3.

You need to add a separator based on your csv file in the str.split method.

You can read more about it here

f1_number = (i.split()[numeric_location])

This is doing a lot in a single line. I suggest you split this into two lines:

f1 = i.split()
f1_number = f1[numeric_location]
f1_date = f1[date_location]

Now you will see which of these causes the problem. You should add a print(f1) to see the value after the split. Most likely it doesn't have as many elements as you think it does. Or your indexes are off from what they should be.

The call to i.split() is going to generate a new list, which will contain each word of the from the string i. So

"this is an example".split() == ["this", "is", "an", "example"]

You are trying to access the third element of the resulting list, and the index error tells you that this list has less than four members. I suggest printing the result of i.split(). Very likely this is either an off by one error, or the first line of your file contains something different than what you are expecting.

Also split() by default will split on whitespace, given that you have a csv you may have wanted to do split(',').

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