How to select every other line from a text file?

Gary Herron gary.herron at islandtraining.com
Mon Oct 13 19:50:43 CEST 2014


On 10/13/2014 10:38 AM, 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?
>
>
> Thanks,

Read in your lines, keeping a counter as you go.  "Select" those lines 
whose counter is even (or odd -- you didn't say which you wanted).

So now some questions for you:

  * Do you know how to open a file and read in all the lines?
  * Do you know how to count as you do so?
  * Do you know how to test for evenness?   (Use count%2 will be zero
    for even count values.)
  * Do you know how to write lines to an output file?

Gary Herron

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20141013/4c51eb57/attachment.html>


More information about the Python-list mailing list