Return-Path: 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 Cc: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Tue, Oct 14, 2014 at 4:38 AM, 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? > 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