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


Groups > comp.lang.python > #79064

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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!nntp.club.cc.cmu.edu!micro-heart-of-gold.mit.edu!bloom-beacon.mit.edu!bloom-beacon.mit.edu!panix!not-for-mail
From Grant Edwards <invalid@invalid.invalid>
Newsgroups comp.lang.python
Subject Re: How to select every other line from a text file?
Date Mon, 13 Oct 2014 19:04:56 +0000 (UTC)
Organization PANIX Public Access Internet and UNIX, NYC
Lines 36
Message-ID <m1h7oo$l7m$1@reader1.panix.com> (permalink)
References <3be64ca8-d2e7-493a-b4f3-ef114f581450@googlegroups.com> <mailman.14826.1413222398.18130.python-list@python.org>
NNTP-Posting-Host dsl.comtrol.com
X-Trace reader1.panix.com 1413227096 21750 64.122.56.22 (13 Oct 2014 19:04:56 GMT)
X-Complaints-To abuse@panix.com
NNTP-Posting-Date Mon, 13 Oct 2014 19:04:56 +0000 (UTC)
User-Agent slrn/1.0.1 (Linux)
Xref csiph.com comp.lang.python:79064

Show key headers only | View raw


On 2014-10-13, Chris Angelico <rosuav@gmail.com> wrote:
> On Tue, Oct 14, 2014 at 4:38 AM, Rff <rwang@avnera.com> wrote:
>> 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?
>>
>
> There are a few ways of doing this. I'm guessing this is probably a
> homework assignment, so I won't give you the code as-is, but here are
> a few ideas:
>
> 1) Iterate over the file (line by line), alternating between writing
>    the line out and not writing the line out.
>
> 2) Read the file into a list of lines, then slice the list with a step
>    of 2, and write those lines out.
>
> 3) Iterate over the file, but also consume an extra line at the top or
>    bottom of the loop.
>
> 4) Read the entire file into a string, then abuse regular expressions
>    violently until they do what you want.

I'd vote for #3.  Or write a generator that does something similar
when given a parameter object that implements readline().

Of course, the _real_ answer is:

os.system("sed -n 'g;n;p' '%s'" % filename)

;)

-- 
Grant Edwards               grant.b.edwards        Yow! Didn't I buy a 1951
                                  at               Packard from you last March
                              gmail.com            in Cairo?

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