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

Dan Liang danliang20 at gmail.com
Tue Apr 28 05:29:13 CEST 2009


Hi Bob, Shantanoo, Kent, and tutors,

Thank you Bob, Shantanoo, Kent for all the nice feedback. Exception
handling, the concept of states in cs, and the use of the for loop with
offset helped a lot. Here is the code I now have, based on your suggestions,
and it does what I need:

ListLines = [ line.rstrip() for line in open('test.txt') ]

countYes = 0
countNo = 0

for i in range(len(ListLines)):
 if ListLines[i].endswith('yes'):
     countYes+=1
     print "countYes", countYes, "\t\t", ListLines[i]

 if not ListLines[i].endswith('yes'):
    continue

 for offset in (1, 2, 3, 4, 5, 6, 7, 8):
    if i+offset < len(ListLines) and ListLines[i+offset].endswith('no'):

       countNo+=1

       print "countNo", countNo, "\t\t", ListLines[i+offset]

Thank you again!

--dan





On Sun, Apr 26, 2009 at 10:55 AM, Kent Johnson <kent37 at tds.net> wrote:

> On Sat, Apr 25, 2009 at 2:11 PM, Dan Liang <danliang20 at gmail.com> wrote:
> > Hi Bob and tutors,
> >
> > Thanks Bob for your response! currently I have the current code, but it
> does
> > not work:
> >
> > ListLines= []
> > for line in open('test.txt'):
> >     line = line.rstrip()
> >     ListLines.append(line)
>
> This could be written with a list comprehension:
> ListLines = [ line.rstrip() for line in open('test.txt') ]
>
> >
> > for i in range(len(ListLines)):
> >
> >     if ListLines[i].endswith("yes") and ListLines[i+1].endswith("no") and
> > ListLines[i+1].endswith("no"):
> >         print ListLines[i], ListLines[i+1], ListLines[i+2]
> >     elif ListLines[i].endswith("yes") and ListLines[i+1].endswith("no"):
> >         print ListLines[i], ListLines[i+1]
> >         elif ListLines[i].endswith("yes"):
> >         print ListLines[i]
> >     elif ListLines[i].endswith("no"):
> >         continue
> >     else:
> >         break
>
> You only need to test for ListLines[i].endswith('yes') once. Then you
> could use a loop to test for lines ending with 'no'.
>
> for i in range(len(ListLines)):
>   if not ListLines[i].endswith('yes'):
>    continue
>  for offset in (1, 2):
>    if i+offset < len(ListLines) and ListLines[i+offset].endswith('no'):
>      print ListLines[i+offset]
>
> You could adapt the above for a variable number of 'no' lines with
> something like
>  for offset in range(1, maxNo+1):
>
> > I get the following error:
> > Traceback (most recent call last):
> >   File "test.py", line 18, in <module>
> >     if ListLines[i].endswith("yes") and ListLines[i+1].endswith("no") and
> > ListLines[i+1].endswith("no"):
> > IndexError: list index out of range
>
> That is because you have a 'yes' line at the end of the file so the
> check for 'no' tries to read past the end of ListLines. In my code the
> test for i+offset < len(ListLines) will prevent that exception.
>
> Kent
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090427/4f20647b/attachment-0001.htm>


More information about the Tutor mailing list