0

I have a CSV and the following code:

with open("results.csv", 'rt', encoding='UTF-8', errors='ignore') as file:
    reader = csv.reader(file, skipinitialspace=True, escapechar='\\')
    A = 0
    for row in reader:
        if row[4] in validclubs and "A" in row[0]:
            A = A + 1
print(A)

but it returns:

Traceback (most recent call last): if row[4] in validclubs and "A" in row[0]: IndexError: list index out of range.

However if I change my code to:

with open("results.csv", 'rt', encoding='UTF-8', errors='ignore') as file:
    reader = csv.reader(file, skipinitialspace=True, escapechar='\\')
    A = 0
    for row in reader:
        if row[4] in validclubs and "A" in row[0]:
            print(A)

It will print(A) correctly and return the same error. Therefore, it's not out of range as it prints properly. So what is causing the error?

1 Answer 1

1

It seems that the error is not caused by the first time the condition is met, but rather by the second (or later) time. Therefore, you manage to print 'A' once, and only then encounter the error.

Sign up to request clarification or add additional context in comments.

1 Comment

The problem was a line at the end of the file contained just ' and caused index error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.