0

I'm trying to figure out how to deal with empty cells when iterating through a CSV file. I am currently receiving an IndexError whenever I run the loop. This is because the rows are not empty, just some of the cells in the columns that I am iterating through.

I am using a standard for loop for the iteration. Is there a way that I can skip the empty cells when running the loop?

Gwart
  • 3
  • 2

1 Answers1

0

You may wish to handle the exception with a try and except block.

try:
    print('This is your code')
except IndexError:
    print('This is where you handle the error if it occurs')

If it is appropriate to ignore the exception, you may use the pass keyword. This is a null statement and is used to maintain proper syntax.

Here is some documentation regarding exceptions management in Python.

  • I think this is close. I'm getting an error with this too. I think the problem is I now have empty values from the empty cells. Is there a way to skip the empty cells instead of bypassing the exception? – Gwart Oct 20 '22 at 21:40