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(&#39;test.txt&#39;):</span><br style="color: rgb(0, 153, 0);"><span style="color: rgb(0, 153, 0);">    line = line.rstrip()</span><br style="color: rgb(0, 153, 0);"><span style="color: rgb(0, 153, 0);">    ListLines.append(line)</span><br style="color: rgb(0, 153, 0);">
<span style="color: rgb(0, 153, 0);"> </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);">    if ListLines[i].endswith(&quot;yes&quot;) and ListLines[i+1].endswith(&quot;no&quot;) and ListLines[i+1].endswith(&quot;no&quot;):</span><br style="color: rgb(0, 153, 0);"><span style="color: rgb(0, 153, 0);">        print ListLines[i], ListLines[i+1], ListLines[i+2]</span><br style="color: rgb(0, 153, 0);">
<span style="color: rgb(0, 153, 0);">    elif ListLines[i].endswith(&quot;yes&quot;) and ListLines[i+1].endswith(&quot;no&quot;):</span><br style="color: rgb(0, 153, 0);"><span style="color: rgb(0, 153, 0);">        print ListLines[i], ListLines[i+1]</span><br style="color: rgb(0, 153, 0);">
<span style="color: rgb(0, 153, 0);">        elif ListLines[i].endswith(&quot;yes&quot;):</span><br style="color: rgb(0, 153, 0);"><span style="color: rgb(0, 153, 0);">        print ListLines[i]</span><br style="color: rgb(0, 153, 0);">
<span style="color: rgb(0, 153, 0);">    elif ListLines[i].endswith(&quot;no&quot;):</span><br style="color: rgb(0, 153, 0);"><span style="color: rgb(0, 153, 0);">        continue</span><br style="color: rgb(0, 153, 0);">
<span style="color: rgb(0, 153, 0);">    else:</span><br style="color: rgb(0, 153, 0);"><span style="color: rgb(0, 153, 0);">        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);">  File &quot;test.py&quot;, line 18, in &lt;module&gt;</span><br style="color: rgb(204, 0, 0);"><span style="color: rgb(204, 0, 0);">    if ListLines[i].endswith(&quot;yes&quot;) and ListLines[i+1].endswith(&quot;no&quot;) and ListLines[i+1].endswith(&quot;no&quot;):</span><br style="color: rgb(204, 0, 0);">
<span style="color: rgb(204, 0, 0);">IndexError: list index out of range</span><br><br>Lines in the file look like following:<br><br><span style="color: rgb(51, 51, 255);">    word1 word2 word3 word4 yes</span><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);">    word1 word2 word3 word4 no</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">    word1 word2 word3 word4 no</span><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);">    word1 word2 word3 word4 no</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">    word1 word2 word3 word4 yes</span><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);">    word1 word2 word3 word4 no</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">    word1 word2 word3 word4 yes</span><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);">    word1 word2 word3 word4 no</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">    word1 word2 word3 word4 yes</span><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);">    word1 word2 word3 word4 yes</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">    word1 word2 word3 word4 no</span><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);">    word1 word2 word3 word4 no</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">    word1 word2 word3 word4 no</span><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);">    word1 word2 word3 word4 yes</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">    word1 word2 word3 word4 no</span><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);">    word1 word2 word3 word4 yes</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">    word1 word2 word3 word4 yes</span><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);">    word1 word2 word3 word4 no</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">    word1 word2 word3 word4 no</span><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);">    word1 word2 word3 word4 no</span><br style="color: rgb(51, 51, 255);"><span style="color: rgb(51, 51, 255);">    word1 word2 word3 word4 no</span><br style="color: rgb(51, 51, 255);">
<span style="color: rgb(51, 51, 255);">    word1 word2 word3 word4 yes</span><br><br><div class="gmail_quote">&gt; What do we do at steps 4ff if the line does not end in &quot;yes&quot; or &quot;no&quot;?<br><br>I forgot to mention that I know for sure that the file has ONLY lines that end in either &quot;yes&quot; or &quot;no&quot;.<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>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>&gt;</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 &quot;yes&quot;, print it.<br>
2) move to the line following the one described in #1 above, and if it ends in, &quot;no&quot; print it.<br>
3) move to third line, and if it ends in &quot;no&quot;, print it.<br>
4) move to fourth line, and if it ends in &quot;no&quot; discard it, but if it ends in &quot;yes&quot; repeat 1, 2, and 3 above.<br>
5) move to fifth line, and if it ends in &quot;no&quot; discard it, but if it ends in &quot;yes&quot; repeat 1, 2, 3, and 4 above, and so on.<br>
<br>
The goal is to get a ratio of 1 to 2 &quot;yes&quot; to &quot;no&quot; 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 &quot;yes&quot; to &quot;no&quot; 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 &quot;yes&quot; or &quot;no&quot;?<br>
<br>
If you have not written any code make a stab at it. You could start by asking &quot;how in Python does one&quot;:<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>