Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #79065

Re: How to select every other line from a text file?

Date 2014-10-13 12:55 -0500
From Tim Chase <python.list@tim.thechases.com>
Subject Re: How to select every other line from a text file?
References <3be64ca8-d2e7-493a-b4f3-ef114f581450@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.14831.1413227348.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 2014-10-13 10:38, Rff 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?

You could force a re-read from the file each line:

  with open("x.txt") as f:
    for line in f:
      do_something(line)
      next(f)  # discard/consume the next line

Or, if you have it read into memory already, you could use slicing
with a stride of 2:

  with open("x.txt") as f:
    data = f.readlines()
    interesting = data[::2] # start with the 1st line
    # interesting = data[1::2] # start with the 2nd line

Or, if the file was large and you didn't want to have it all in
memory at the same time, you could use itertools.islice()

  from itertools import islice
  with open("x.txt") as f:
    interesting = islice(f, 0, None, 2) # start with 1st line
    #interesting = islice(f, 1, None, 2) # start with 2nd line

Note that in the last one, you get an iterator back, so you'd have to
either turn it into a list or iterate over it.

-tkc


Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How to select every other line from a text file? Rff <rwang@avnera.com> - 2014-10-13 10:38 -0700
  Re: How to select every other line from a text file? Chris Angelico <rosuav@gmail.com> - 2014-10-14 04:46 +1100
    Re: How to select every other line from a text file? Grant Edwards <invalid@invalid.invalid> - 2014-10-13 19:04 +0000
  Re: How to select every other line from a text file? John Gordon <gordon@panix.com> - 2014-10-13 17:48 +0000
    Re: How to select every other line from a text file? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-13 19:02 +0100
    Re: How to select every other line from a text file? emile <emile@fenx.com> - 2014-10-13 11:11 -0700
    Re: How to select every other line from a text file? Joel Goldstick <joel.goldstick@gmail.com> - 2014-10-13 14:45 -0400
    Re: How to select every other line from a text file? Tim Chase <python.list@tim.thechases.com> - 2014-10-13 14:12 -0500
    Re: How to select every other line from a text file? emile <emile@fenx.com> - 2014-10-13 13:01 -0700
  Re: How to select every other line from a text file? Gary Herron <gary.herron@islandtraining.com> - 2014-10-13 10:50 -0700
  Re: How to select every other line from a text file? Tim Chase <python.list@tim.thechases.com> - 2014-10-13 12:55 -0500
  Re: How to select every other line from a text file? Denis McMahon <denismfmcmahon@gmail.com> - 2014-10-13 23:54 +0000
  Re: How to select every other line from a text file? Dan Stromberg <drsalists@gmail.com> - 2014-10-13 17:34 -0700

csiph-web