[Tutor] Re: file filter

Jeff Shannon jeff@ccvcorp.com
Wed, 26 Dec 2001 10:05:38 -0800


> "kevin parks" <kp87@lycos.com> asked:

> Happy Holidays. I am trying to write a some python code that will copy a file to a new file but with certain lines filtered out. Let's say that i want the new file to have all lines except those that start with a semicolon or a letter c.  ....

> i can get the copy part. It is just the filter part i am confused about. You see, if the line begins with 'c' or ';' i want to ignore the whole line.
>
> cheers,
>
> kevin
>
> def boksa(infilename, outfilename):
>         """this will copy a file exactly, args are: 'infile' and 'outfile'"""
>         infile = open(infilename, 'r')
>         f = open(outfilename, 'w')
>         for aLine in infile.xreadlines() :
>                 f.write( aLine )
>         infile.close()
>         f.close()
>
> if __name__ == '__main__':
>         boksa()

Karthik Gurumurthy already gave you one option, by mentioning the startswith() string method.  You can also use string slicing and 'if something in somelist' to good effect here.

def boksa(infilename, outfilename, filterchars):
    """Reads file infilename, copying each line to file outfilename,
       except for lines that start with characters in the list filterchars."""
    infile = open(infilename, 'r')
    outfile = open(outfilename, 'w')
    for line in infile.xreadlines():
        startchar = line[0]    # assign the first char in line to startchar
        if startchar not in filterchars:
            outfile.write(line)
    infile.close()
    outfile.close()

if __name__ == '__main__':
    myfile = 'test.txt'
    newfile = 'filtered-' + myfile
    boksa( myfile, newfile, [ 'c', ';' ] )

This will let you filter out lines that start with just about any character, and it's really easy to add more filter-characters -- just specify them in the list that you pass to the function.  (Note also that in your original example, when you called boksa() you forgot to pass it any
filenames--I've corrected that in this example.)

Hope that helps...

Jeff Shannon
Technician/Programmer
Credit International