Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #79057
| Return-Path | <rosuav@gmail.com> |
|---|---|
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.003 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'subject:text': 0.05; 'abuse': 0.07; 'expressions': 0.07; 'lines,': 0.07; 'subject:file': 0.07; 'iterate': 0.09; 'subject:How': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'guessing': 0.16; 'it;': 0.16; 'line),': 0.16; 'loop.': 0.16; 'task.': 0.16; 'wrote:': 0.18; 'file,': 0.19; 'select': 0.22; 'cc:addr:python.org': 0.22; 'this?': 0.23; 'string,': 0.24; 'fairly': 0.24; 'file.': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'lines': 0.31; 'too.': 0.31; 'subject:other': 0.31; 'file': 0.32; 'this.': 0.32; 'probably': 0.32; 'regular': 0.32; 'text': 0.33; 'beginning': 0.33; 'subject:from': 0.34; 'could': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; '14,': 0.36; 'doing': 0.36; 'subject:?': 0.36; 'list': 0.37; 'step': 0.37; 'read': 0.60; 'tell': 0.60; 'new': 0.61; 'entire': 0.61; 'show': 0.63; 'here': 0.66; 'between': 0.67; 'bottom': 0.67; 'lack': 0.78; 'homework': 0.84; 'to:none': 0.92 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=fcU7V0QW8adtRTSEYTUVGQeigK89YNqAqojpcDOaQ0g=; b=wzifP9aqEg1FkgaebF2K7fCJu/ccrPnvESUnKO/dv2+31BnQ7HhVS5D2hUKFqO2ZNC TH3gs9+Vd9gUeWR1mY28zbn5gIubwu1ys3GATjz3vmTyX83ZPsvC8jAjb2NArcVOa4m8 myb9uOzzs9nQg1mLivluzvgE+IfRo/DZEXb/RivO/8uXG4VUUXOG8NOlFKyb+DjGAlHA mmXUMKiNUdW6XJtqSEzB7gqrMpga4CZOEwqV53UD3cubAxyvoVfSucZLeemheMXocpU0 mBRPy5bEuKklZ+uPzSQRIh5Canyca5EyUOA7AdjpRGbEthsGQAuc20HZugewb7wwnxpm hrrw== |
| MIME-Version | 1.0 |
| X-Received | by 10.42.37.138 with SMTP id y10mr142983icd.26.1413222393939; Mon, 13 Oct 2014 10:46:33 -0700 (PDT) |
| In-Reply-To | <3be64ca8-d2e7-493a-b4f3-ef114f581450@googlegroups.com> |
| References | <3be64ca8-d2e7-493a-b4f3-ef114f581450@googlegroups.com> |
| Date | Tue, 14 Oct 2014 04:46:33 +1100 |
| Subject | Re: How to select every other line from a text file? |
| From | Chris Angelico <rosuav@gmail.com> |
| Cc | "python-list@python.org" <python-list@python.org> |
| Content-Type | text/plain; charset=UTF-8 |
| 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.14826.1413222398.18130.python-list@python.org> (permalink) |
| Lines | 23 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1413222398 news.xs4all.nl 2946 [2001:888:2000:d::a6]:59738 |
| X-Complaints-To | abuse@xs4all.nl |
| Path | csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
| Xref | csiph.com comp.lang.python:79057 |
Show key headers only | View raw
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
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