Question on loops
Delaney, Timothy C (Timothy)
tdelaney at avaya.com
Mon Mar 8 15:19:49 EST 2004
> From: Daniel H Neilson
>
> > If you're using Python 2.3.x
> >
> > for i, line in enumerate(file):
> > if i % 100 == 0:
> > action
> >
> > process(line)
> >
> > Tim Delaney
>
> Beautiful. Thanks to those who responded. DHN
Note that as Sean pointed out, you will need to add 1 before performing the modulo if you want the action to be performed on the 100th line. As the above stands, the action will be performed on lines [1, 101, 201, ...].
So the test should either be:
if i % 100 == 99:
or
if (i + 1) % 100 == 0:
which will give you the action on lines [100, 200, 300, ...].
Tim Delaney
More information about the Python-list
mailing list