[Tutor] Working with lines from file and printing to another keeping sequential order

Dan Liang danliang20 at gmail.com
Sat Apr 25 16:01:29 CEST 2009


Dear Tutors,


I have a file from which I want to extract lines that end in certain strings
and print to a second file. More specifically, I want to:

1) iterate over each line in the file, and if it ends in "yes", print it.
2) move to the line following the one described in #1 above, and if it ends
in, "no" print it.
3) move to third line, and if it ends in "no", print it.
4) move to fourth line, and if it ends in "no" discard it, but if it ends in
"yes" repeat 1, 2, and 3 above.
5) move to fifth line, and if it ends in "no" discard it, but if it ends in
"yes" repeat 1, 2, 3, and 4 above, and so on.

The goal is to get a ratio of 1 to 2 "yes" to "no" lines from a file in such
a way that keeps the order of the lines in output. An abstraction away from
this so that any ratio of "yes" to "no" lines could be printed while keeping
the order of the original lines would be great.


I am new to Python and could not solve the problem. Your help is
appreciated.

Cheers,

--dan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090425/691d0192/attachment.htm>


More information about the Tutor mailing list