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


Groups > comp.lang.python > #79056 > unrolled thread

How to select every other line from a text file?

Started byRff <rwang@avnera.com>
First post2014-10-13 10:38 -0700
Last post2014-10-13 17:34 -0700
Articles 13 — 11 participants

Back to article view | Back to comp.lang.python


Contents

  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

#79056 — How to select every other line from a text file?

FromRff <rwang@avnera.com>
Date2014-10-13 10:38 -0700
SubjectHow to select every other line from a text file?
Message-ID<3be64ca8-d2e7-493a-b4f3-ef114f581450@googlegroups.com>
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?


Thanks,

[toc] | [next] | [standalone]


#79057

FromChris Angelico <rosuav@gmail.com>
Date2014-10-14 04:46 +1100
Message-ID<mailman.14826.1413222398.18130.python-list@python.org>
In reply to#79056
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.

And there are other ways, too. Show us some code and we can help you
with it; but at the moment, this is fairly open-ended.

ChrisA

[toc] | [prev] | [next] | [standalone]


#79064

FromGrant Edwards <invalid@invalid.invalid>
Date2014-10-13 19:04 +0000
Message-ID<m1h7oo$l7m$1@reader1.panix.com>
In reply to#79057
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?

[toc] | [prev] | [next] | [standalone]


#79058

FromJohn Gordon <gordon@panix.com>
Date2014-10-13 17:48 +0000
Message-ID<m1h39u$412$1@reader1.panix.com>
In reply to#79056
In <3be64ca8-d2e7-493a-b4f3-ef114f581450@googlegroups.com> Rff <rwang@avnera.com> 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.

-- 
John Gordon         Imagine what it must be like for a real medical doctor to
gordon@panix.com    watch 'House', or a real serial killer to watch 'Dexter'.

[toc] | [prev] | [next] | [standalone]


#79060

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-10-13 19:02 +0100
Message-ID<mailman.14828.1413223364.18130.python-list@python.org>
In reply to#79058
On 13/10/2014 18:48, John Gordon wrote:
> In <3be64ca8-d2e7-493a-b4f3-ef114f581450@googlegroups.com> Rff <rwang@avnera.com> 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

[toc] | [prev] | [next] | [standalone]


#79061

Fromemile <emile@fenx.com>
Date2014-10-13 11:11 -0700
Message-ID<mailman.14829.1413223953.18130.python-list@python.org>
In reply to#79058
On 10/13/2014 11:02 AM, Mark Lawrence wrote:

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

I see it as a question of addressing the audience.

Emile


[toc] | [prev] | [next] | [standalone]


#79063

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2014-10-13 14:45 -0400
Message-ID<mailman.14830.1413225956.18130.python-list@python.org>
In reply to#79058
On Mon, Oct 13, 2014 at 2:11 PM, emile <emile@fenx.com> wrote:
> On 10/13/2014 11:02 AM, Mark Lawrence wrote:
>
>> Why bother to initialise a counter when you can get the enumerate
>> function to do all the work for you?
>
>
> I see it as a question of addressing the audience.
>
> Emile

I don't agree with the idea of using a counter.  Its not pythonic, and
I'm assuming the OP is just starting to learn python.

Not apropos to the OP, but what came up in my mind was to write a
generator function that returns every other line.  This would separate
the reading from the writing code.

>
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Joel Goldstick
http://joelgoldstick.com

[toc] | [prev] | [next] | [standalone]


#79066

FromTim Chase <python.list@tim.thechases.com>
Date2014-10-13 14:12 -0500
Message-ID<mailman.14832.1413229504.18130.python-list@python.org>
In reply to#79058
On 2014-10-13 14:45, Joel Goldstick wrote:
> Not apropos to the OP, but what came up in my mind was to write a
> generator function that returns every other line.  This would
> separate the reading from the writing code.

You mean like

  offset = 0 # or 1 if you prefer
  for line in itertools.islice(source_iter, offset, None, 2):
    do_something(line)

?

-tkc

[toc] | [prev] | [next] | [standalone]


#79067

Fromemile <emile@fenx.com>
Date2014-10-13 13:01 -0700
Message-ID<mailman.14833.1413230517.18130.python-list@python.org>
In reply to#79058
On 10/13/2014 12:12 PM, Tim Chase wrote:

> You mean like
>
>    offset = 0 # or 1 if you prefer
>    for line in itertools.islice(source_iter, offset, None, 2):
>      do_something(line)


I certainly did.  Learning the python standard library is different from 
learning python and each in its own time.

Emile



[toc] | [prev] | [next] | [standalone]


#79059

FromGary Herron <gary.herron@islandtraining.com>
Date2014-10-13 10:50 -0700
Message-ID<mailman.14827.1413223031.18130.python-list@python.org>
In reply to#79056

[Multipart message — attachments visible in raw view] — view raw

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

Read in your lines, keeping a counter as you go.  "Select" those lines 
whose counter is even (or odd -- you didn't say which you wanted).

So now some questions for you:

  * Do you know how to open a file and read in all the lines?
  * Do you know how to count as you do so?
  * Do you know how to test for evenness?   (Use count%2 will be zero
    for even count values.)
  * Do you know how to write lines to an output file?

Gary Herron

[toc] | [prev] | [next] | [standalone]


#79065

FromTim Chase <python.list@tim.thechases.com>
Date2014-10-13 12:55 -0500
Message-ID<mailman.14831.1413227348.18130.python-list@python.org>
In reply to#79056
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


[toc] | [prev] | [next] | [standalone]


#79072

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-10-13 23:54 +0000
Message-ID<m1hon0$hfb$1@dont-email.me>
In reply to#79056
On Mon, 13 Oct 2014 10:38:48 -0700, Rff 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?

So this could be written as an algorithm something like:

1/ open the input file
2/ open the output file
3/ while there are lines to read from the input file
	3/1/ read a line from the input file
	3/2/ if I should output this line
		3/2/1/ write line to output file
4/ close the input file
5/ close the output file

Or in several other ways, and once you have an algorithm, you can start 
coding it (or implementing it in the programming language of your choice, 
whichever form of words best pleases your perfesser).

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#79073

FromDan Stromberg <drsalists@gmail.com>
Date2014-10-13 17:34 -0700
Message-ID<mailman.14838.1413246898.18130.python-list@python.org>
In reply to#79056
On Mon, Oct 13, 2014 at 10:38 AM, Rff <rwang@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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web