Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!bcyclone01.am1.xlned.com!bcyclone01.am1.xlned.com!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'from:addr:yahoo.co.uk': 0.04; 'subject:text': 0.05; 'initialize': 0.07; 'odd': 0.07; 'subject:file': 0.07; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:How': 0.10; 'python': 0.11; 'language.': 0.14; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'task.': 0.16; 'zero.': 0.16; 'language': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'file,': 0.19; 'select': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'file.': 0.24; '(or': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'function': 0.29; 'statement': 0.30; 'you?': 0.31; 'subject:other': 0.31; 'writes:': 0.31; 'another': 0.32; 'text': 0.33; 'beginning': 0.33; 'subject:from': 0.34; 'could': 0.34; 'one,': 0.35; 'but': 0.35; 'add': 0.35; 'subject:?': 0.36; 'hi,': 0.36; 'to:addr:python- list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'read': 0.60; 'tell': 0.60; 'new': 0.61; 'john': 0.61; 'our': 0.64; 'charset:windows-1252': 0.65; 'lack': 0.78 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: How to select every other line from a text file? Date: Mon, 13 Oct 2014 19:02:24 +0100 References: <3be64ca8-d2e7-493a-b4f3-ef114f581450@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-78-147-30-10.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.1.2 In-Reply-To: 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: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1413223364 news.xs4all.nl 2930 [2001:888:2000:d::a6]:39763 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 3923 X-Received-Body-CRC: 2471701822 Xref: csiph.com comp.lang.python:79060 On 13/10/2014 18:48, John Gordon wrote: > In <3be64ca8-d2e7-493a-b4f3-ef114f581450@googlegroups.com> Rff 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