1

Reading this post didn't really answer my question.

I have a

reader = csv.reader(source)
for row in reader:
    if len(row[0]) > 23:
        # Do stuff
        continue
    if low > float(row[1]) > high:
        # Do stuff
        continue
    if low > float(row[2]) > high:
        # Do stuff
        continue
else:
    print('All', reader.line_num, 'read successfully.')

setup, but the else is executed despite me skipping in the for loop.

I'd rather that the else was only called if no continue was hit.

In order to clarify, the purpose of the code is to drop bad/malformed data rows in the CSV file. As such, malformed lines have individual error handling. Using 'else' as final notifier would, if possible be much more beautiful than working with flags.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pebermynte Lars
  • 414
  • 3
  • 14

1 Answers1

4

The only thing that matters to else is whether or not the loop terminated because you reached the end of the iterable; it doesn't care what happens inside the loop.

If you want to have different behavior depending on if continue was ever called, then you have to keep track of that yourself.