[Tutor] Searching in a file

Paul Melvin paul at assured-networks.co.uk
Wed Jan 13 18:49:23 CET 2010


I have a file generated from a webpage.

I want to search that file for a specific keyword, in my case 'NEW'.

Once I have found that keyword I want to retrieve information below it, e.g.
web link, size of file etc.

When I have this information I move off until I find another 'NEW' and the
process starts all over.

Please can someone give me some pointers on how to do this.

I can find the line containing 'NEW' which I get using a simple for loop on
every line in the file, but how do I then traverse the next x amount of
lines, taking what I want until either the next 'NEW' or eof.


for line in file:
	if re.findall('NEW', line)	# or search
		list.append(line)		# to do something to later

I cannot 'get' to the following lines because I would need to get out of the

I did use enumerate and was wondering if I could use the line number, or
maybe I could use an re iterator.

Any advice would be much appreciated.



__________ Information from ESET Smart Security, version of virus signature
database 4767 (20100113) __________

The message was checked by ESET Smart Security.


More information about the Tutor mailing list