simple text filter

Helmut Jarausch jarausch at skynet.be
Wed Jun 11 17:13:15 EDT 2003


boutrosp at hotmail.com 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 :

replace this with
           elif junky == '' :
since readline returns an empty string on EOF

>                 done = True
>         else :
>                 print junky
> 
> f.close()


-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany





More information about the Python-list mailing list