[Tutor] Re: file filter

Karthik Gurumurthy karthikg@aztec.soft.net
Wed, 26 Dec 2001 19:25:02 +0530


startswith method should help here..

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() :
		if not (aLine.startswith(';') or aLine.startswith('c')):
			f.write( aLine )
	infile.close()
	f.close()

string module has lots of helpful methods.

karthik.

-----Original Message-----
From: tutor-admin@python.org [mailto:tutor-admin@python.org]On Behalf Of
kevin parks
Sent: Wednesday, December 26, 2001 6:59 PM
To: tutor@python.org
Subject: [Tutor] Re: file filter


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

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor