Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #79065
| Path | csiph.com!usenet.pasdenom.info!poup.poupinou.org!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python.list@tim.thechases.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'subject:text': 0.05; 'none,': 0.07; 'subject:file': 0.07; 'iterate': 0.09; 'line:': 0.09; 'subject:How': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; '-tkc': 0.16; 'already,': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'iterator': 0.16; 'itertools': 0.16; 're-read': 0.16; 'stride': 0.16; 'task.': 0.16; 'wrote:': 0.18; 'memory': 0.22; 'select': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'this?': 0.23; 'file.': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'subject:other': 0.31; 'file': 0.32; 'text': 0.33; 'beginning': 0.33; 'subject:from': 0.34; 'could': 0.34; 'one,': 0.35; 'but': 0.35; 'next': 0.36; "didn't": 0.36; 'charset:us-ascii': 0.36; 'subject:?': 0.36; 'hi,': 0.36; 'turn': 0.37; 'list': 0.37; 'received:10': 0.37; 'either': 0.39; 'read': 0.60; '2nd': 0.60; 'tell': 0.60; 'new': 0.61; '1st': 0.74; 'lack': 0.78 |
| X-Sender-Id | wwwh|x-authuser|tim@thechases.com |
| X-Sender-Id | wwwh|x-authuser|tim@thechases.com |
| X-MC-Relay | Neutral |
| X-MailChannels-SenderId | wwwh|x-authuser|tim@thechases.com |
| X-MailChannels-Auth-Id | wwwh |
| X-MC-Loop-Signature | 1413222772853:377706112 |
| X-MC-Ingress-Time | 1413222772829 |
| Date | Mon, 13 Oct 2014 12:55:55 -0500 |
| From | Tim Chase <python.list@tim.thechases.com> |
| To | Rff <rwang@avnera.com> |
| Subject | Re: How to select every other line from a text file? |
| In-Reply-To | <3be64ca8-d2e7-493a-b4f3-ef114f581450@googlegroups.com> |
| References | <3be64ca8-d2e7-493a-b4f3-ef114f581450@googlegroups.com> |
| X-Mailer | Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=US-ASCII |
| Content-Transfer-Encoding | 7bit |
| X-AuthUser | tim@thechases.com |
| Cc | python-list@python.org |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.14831.1413227348.18130.python-list@python.org> (permalink) |
| Lines | 37 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1413227348 news.xs4all.nl 2892 [2001:888:2000:d::a6]:56696 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:79065 |
Show key headers only | 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
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