How to select every other line from a text file?
gordon at panix.com
Mon Oct 13 19:48:46 CEST 2014
In <3be64ca8-d2e7-493a-b4f3-ef114f581450 at googlegroups.com> Rff <rwang at avnera.com> writes:
> I have a text file. Now it is required to select every other line of that
> text to generate a new text file. I have read through Python grammar, but
> still lack the idea at the beginning of the task. Could you tell me some
> methods to get this?
Initialize a counter variable to zero. (Or one, depending if you want to
select odd or even lines.)
Each time you read a line from the file, add one to the counter.
If the counter is odd, process the line; otherwise use the 'continue'
statement to start the loop over and read another line.
John Gordon Imagine what it must be like for a real medical doctor to
gordon at panix.com watch 'House', or a real serial killer to watch 'Dexter'.
More information about the Python-list