simple text filter

Curly Joe wooooee at yahoo.com
Thu Jun 12 03:00:51 CEST 2003


FYI - here is an alternate method, but the test for
an empty string is more straight forward IMHO because
there is only one read statement and it is within 
the while loop.  Note that in your code, the EOF
will be processed like a regular line of data, but
nothing will be done in this case because you will be
searching an empty string.  This is not always the 
case though, so try to think in terms of how the
program executes, which is very difficult for us
transistor-challenged beings.  
 
f=open('adt100_0489.rpt.txt', 'r')
junky = f.readline()
while( junky ) :
         ##  statements should be before the
         ##  readline because it will return an 
         ##  empty string at EOF
         junky = f.readline()

Keep on keeping on though.

> --- In python-list at yahoogroups.com, boutrosp at h...
> wrote:
> > I need some help on a simple text filter. The
> problem I am having is
> > when the file comes to the end it stays in the
> while loop and does 
> not
> > exit. I cannot figure this out. I would use a for
> loop with the
> > readlines() but my datasets can range from 5 to 80
> MB of text data.
> > Here is the code I am using. Please help.
> > 
> > import sys, re
> > 
> > p1 = re.compile('ADT100')
> > p2 = re.compile('AUDIT')
> > p3 = re.compile('HARDWARE')
> > p4 = re.compile('PACKAGES')
> > p5 = re.compile('NODE')
> > p6 = re.compile('DROP')
> > p7 = re.compile('GRID')
> > p8 = re.compile('ATAP')
> > 
> > f=open('adt100_0489.rpt.txt', 'r')
> > junky = 1
> > done = False
> > while not done :
> >         junky = f.readline()
> >         if p1.search(junky) :
> >                 continue
> >         elif p2.search(junky) :
> >                 continue
> >         elif p3.search(junky) :
> >                 continue
> >         elif p4.search(junky) :
> >                 continue
> >         elif p5.search(junky) :
> >                 continue
> >         elif p6.search(junky) :
> >                 continue
> >         elif p7.search(junky) :
> >                 continue
> >         elif p8.search(junky) :
> >                 continue
> >         elif junky == None :
> >                 done = True
> >         else :
> >                 print junky
> > 
> > f.close()
> > -- 
> >
> http://mail.python.org/mailman/listinfo/python-list
> 


__________________________________
Do you Yahoo!?
Yahoo! Calendar - Free online calendar with sync to Outlook(TM).
http://calendar.yahoo.com





More information about the Python-list mailing list