Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!bloom-beacon.mit.edu!bloom-beacon.mit.edu!panix!gordon From: John Gordon Newsgroups: comp.lang.python Subject: Re: How to select every other line from a text file? Date: Mon, 13 Oct 2014 17:48:46 +0000 (UTC) Organization: PANIX Public Access Internet and UNIX, NYC Lines: 20 Message-ID: References: <3be64ca8-d2e7-493a-b4f3-ef114f581450@googlegroups.com> NNTP-Posting-Host: panix1.panix.com X-Trace: reader1.panix.com 1413222526 4130 166.84.1.1 (13 Oct 2014 17:48:46 GMT) X-Complaints-To: abuse@panix.com NNTP-Posting-Date: Mon, 13 Oct 2014 17:48:46 +0000 (UTC) User-Agent: nn/6.7.3 Xref: csiph.com comp.lang.python:79058 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. -- 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'.