<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On 25-Apr-09, at 11:41 PM, Dan Liang wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">Hi Bob and tutors,<br><br>Thanks Bob for your response! currently I have the current code, but it does not work:<br><br style="color: rgb(0, 153, 0);"><span style="color: rgb(0, 153, 0);">ListLines= []</span><br style="color: rgb(0, 153, 0);"> <span style="color: rgb(0, 153, 0);">for line in open('test.txt'):</span><br style="color: rgb(0, 153, 0);"><span style="color: rgb(0, 153, 0);">&nbsp;&nbsp;&nbsp; line = line.rstrip()</span><br style="color: rgb(0, 153, 0);"><span style="color: rgb(0, 153, 0);">&nbsp;&nbsp;&nbsp; ListLines.append(line)</span><br style="color: rgb(0, 153, 0);"> <span style="color: rgb(0, 153, 0);">&nbsp;</span><br style="color: rgb(0, 153, 0);"><span style="color: rgb(0, 153, 0);">for i in range(len(ListLines)):</span><br style="color: rgb(0, 153, 0);"><br style="color: rgb(0, 153, 0);"> <span style="color: rgb(0, 153, 0);">&nbsp;&nbsp;&nbsp; if ListLines[i].endswith("yes") and ListLines[i+1].endswith("no") and ListLines[i+1].endswith("no"):</span><br style="color: rgb(0, 153, 0);"><span style="color: rgb(0, 153, 0);">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; print ListLines[i], ListLines[i+1], ListLines[i+2]</span><br style="color: rgb(0, 153, 0);"> <span style="color: rgb(0, 153, 0);">&nbsp;&nbsp;&nbsp; elif ListLines[i].endswith("yes") and ListLines[i+1].endswith("no"):</span><br style="color: rgb(0, 153, 0);"><span style="color: rgb(0, 153, 0);">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; print ListLines[i], ListLines[i+1]</span><br style="color: rgb(0, 153, 0);"> <span style="color: rgb(0, 153, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elif ListLines[i].endswith("yes"):</span><br style="color: rgb(0, 153, 0);"><span style="color: rgb(0, 153, 0);">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; print ListLines[i]</span><br style="color: rgb(0, 153, 0);"> <span style="color: rgb(0, 153, 0);">&nbsp;&nbsp;&nbsp; elif ListLines[i].endswith("no"):</span><br style="color: rgb(0, 153, 0);"><span style="color: rgb(0, 153, 0);">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; continue</span><br style="color: rgb(0, 153, 0);"> <span style="color: rgb(0, 153, 0);">&nbsp;&nbsp;&nbsp; else:</span><br style="color: rgb(0, 153, 0);"><span style="color: rgb(0, 153, 0);">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; break</span><br><br>I get the following error:<br><span style="color: rgb(204, 0, 0);">Traceback (most recent call last):</span><br style="color: rgb(204, 0, 0);"> <span style="color: rgb(204, 0, 0);">&nbsp; File "test.py", line 18, in &lt;module></span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);">&nbsp;&nbsp;&nbsp; if ListLines[i].endswith("yes") and ListLines[i+1].endswith("no") and ListLines[i+1].endswith("no"):</span><br style="color: rgb(204, 0, 0);"> <span style="color: rgb(204, 0, 0);">IndexError: list index out of range</span></blockquote><div><br></div>You need to put check for&nbsp;</div><div><br></div><div>i &lt;= len(ListLines)-2</div><div><br></div><div><br></div><div>or you can have a look @ try and except (exception handling in python)</div><div><br></div><div><blockquote type="cite">Lines in the file look like following:<br><br><span style="color: rgb(51, 51, 255);">&nbsp;&nbsp;&nbsp; word1 word2 word3 word4 yes</span><br style="color: rgb(51, 51, 255);"> <span style="color: rgb(51, 51, 255);">&nbsp;&nbsp;&nbsp; word1 word2 word3 word4 no</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">&nbsp;&nbsp;&nbsp; word1 word2 word3 word4 no</span><br style="color: rgb(51, 51, 255);"> <span style="color: rgb(51, 51, 255);">&nbsp;&nbsp;&nbsp; word1 word2 word3 word4 no</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">&nbsp;&nbsp;&nbsp; word1 word2 word3 word4 yes</span><br style="color: rgb(51, 51, 255);"> <span style="color: rgb(51, 51, 255);">&nbsp;&nbsp;&nbsp; word1 word2 word3 word4 no</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">&nbsp;&nbsp;&nbsp; word1 word2 word3 word4 yes</span><br style="color: rgb(51, 51, 255);"> <span style="color: rgb(51, 51, 255);">&nbsp;&nbsp;&nbsp; word1 word2 word3 word4 no</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">&nbsp;&nbsp;&nbsp; word1 word2 word3 word4 yes</span><br style="color: rgb(51, 51, 255);"> <span style="color: rgb(51, 51, 255);">&nbsp;&nbsp;&nbsp; word1 word2 word3 word4 yes</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">&nbsp;&nbsp;&nbsp; word1 word2 word3 word4 no</span><br style="color: rgb(51, 51, 255);"> <span style="color: rgb(51, 51, 255);">&nbsp;&nbsp;&nbsp; word1 word2 word3 word4 no</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">&nbsp;&nbsp;&nbsp; word1 word2 word3 word4 no</span><br style="color: rgb(51, 51, 255);"> <span style="color: rgb(51, 51, 255);">&nbsp;&nbsp;&nbsp; word1 word2 word3 word4 yes</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">&nbsp;&nbsp;&nbsp; word1 word2 word3 word4 no</span><br style="color: rgb(51, 51, 255);"> <span style="color: rgb(51, 51, 255);">&nbsp;&nbsp;&nbsp; word1 word2 word3 word4 yes</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">&nbsp;&nbsp;&nbsp; word1 word2 word3 word4 yes</span><br style="color: rgb(51, 51, 255);"> <span style="color: rgb(51, 51, 255);">&nbsp;&nbsp;&nbsp; word1 word2 word3 word4 no</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">&nbsp;&nbsp;&nbsp; word1 word2 word3 word4 no</span><br style="color: rgb(51, 51, 255);"> <span style="color: rgb(51, 51, 255);">&nbsp;&nbsp;&nbsp; word1 word2 word3 word4 no</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">&nbsp;&nbsp;&nbsp; word1 word2 word3 word4 no</span><br style="color: rgb(51, 51, 255);"> <span style="color: rgb(51, 51, 255);">&nbsp;&nbsp;&nbsp; word1 word2 word3 word4 yes</span><br><br><div class="gmail_quote">> What do we do at steps 4ff if the line does not end in "yes" or "no"?<br><br>I forgot to mention that I know for sure that the file has ONLY lines that end in either "yes" or "no".<br> <br>Any suggestions are appreciated. Also, I feel that my code is not the best way of solving the problem even if the problem of list indices is solved. Is my guess right?<br><br></div></blockquote><div><br></div><div><br></div><div>Another suggestion. I would not read all data from file into array (memory). If your file is large, you may hit out of memory error.</div><div><br></div><div>regards,</div><div>shantanoo</div><br><blockquote type="cite"><div class="gmail_quote">Thank you!<br><br>-dan <br><br>On Sat, Apr 25, 2009 at 10:32 AM, bob gailer <span dir="ltr">&lt;<a href="mailto:bgailer@gmail.com">bgailer@gmail.com</a>></span> wrote:<br> <blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div><div></div><div class="h5">Dan Liang wrote:<br> <blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> Dear Tutors,<br> <br> <br> 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:<br> <br> 1) iterate over each line in the file, and if it ends in "yes", print it.<br> 2) move to the line following the one described in #1 above, and if it ends in, "no" print it.<br> 3) move to third line, and if it ends in "no", print it.<br> 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.<br> 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.<br> <br> 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. <br> </blockquote> <br></div></div> Please show us what code you have written, and in what way it fails to meet your expectations.<br> <br> Your specification is IMHO a nice piece of pseudocode which could translate to Python fairly easily!<br> <br> What do we do at steps 4ff if the line does not end in "yes" or "no"?<br> <br> If you have not written any code make a stab at it. You could start by asking "how in Python does one":<br> open a file?<br> iterate (loop)?<br> get the next line from a file?<br> test for equality?<br> examine the end of a string?<br><font color="#888888"> <br> <br> -- <br> Bob Gailer<br> Chapel Hill NC<br> 919-636-4239<br> </font></blockquote></div><br> _______________________________________________<br>Tutor maillist &nbsp;- &nbsp;<a href="mailto:Tutor@python.org">Tutor@python.org</a><br><a href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</a><br></blockquote></div><br></body></html>