[Tutor] Re: file filter

kevin parks kp87@lycos.com
Wed, 26 Dec 2001 22:29:21 +0900


Hi all,

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. So that an input file that has these lines:

;i1 0 1 2 3 2
i1 6 8 7 9 
ci2 99 0 0 0 2
i1 2 3 4
i2 3 4 4 
ci1 3 4 4 5
;i3 929 92 2
i4 2 8 9 1

would yield:

i1 2 3 4
i2 3 4 4 
i4 2 8 9 1

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()






--
Click here for your very own create-a-date adventure from MatchMaker
Go to http://ecard.matchmaker.com/dating.html