How to select every other line from a text file?

Mark Lawrence breamoreboy at
Mon Oct 13 20:02:24 CEST 2014

On 13/10/2014 18:48, John Gordon wrote:
> In <3be64ca8-d2e7-493a-b4f3-ef114f581450 at> Rff <rwang at> writes:
>> Hi,
>> 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.

Why bother to initialise a counter when you can get the enumerate 
function to do all the work for you?

My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

More information about the Python-list mailing list