[Tutor] Searching in a file

wesley chun wescpy at gmail.com
Wed Jan 13 21:16:53 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.
>       :
> Any advice would be much appreciated.


based on your requirements, i think you should abandon the use of 're'
and regular expressions in general since you aren't matching a
pattern. just use the 'in' operator or any string methods, e.g.,
{,r}{find,index}(). it sounds like "NEW" only appears once on any
given line, so those should suffice. if not, you'll have to do further
checks. given those clues, you should be able to accomplish your task
be iterating over each line and processing it as desired.

good luck!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
    http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com


More information about the Tutor mailing list