How to select every other line from a text file?

Dan Stromberg drsalists at gmail.com
Tue Oct 14 02:34:49 CEST 2014


On Mon, Oct 13, 2014 at 10:38 AM, Rff <rwang at avnera.com> wrote:
> 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?

Perhaps something like:
http://stromberg.dnsalias.org/svn/every-nth/trunk

It uses zip and itertools.cycle.

It's CPython 3.x though - if you need 2.x, you'd probably use xrange
instead of range, and izip instead of zip.



More information about the Python-list mailing list