Equivalent string.find method for a list of strings

harold fellermann harold.fellermann at upf.edu
Fri Apr 8 15:37:37 EDT 2005


> I have read a text file using the command
>
> lines = myfile.readlines()
>
> and now I want to seach those lines for a particular string.  I was 
> hoping there was a way to "find" that string in a similar way as 
> searching simply a simple string.  I want to do something like
>
> lines.find.('my particular string')
>
> Is there a module that already exists that allows me to do this or 
> will I have to created my own method?

file.readlines() returns a list of lines. You can either call find on 
each
element in this list, like:

for line in myfile.readlines() :
     if line.find('my particular string') :
         do_something()

of just read in the whole file, if you are not interested in the 
particular
line, where 'my particular string occurs':

myfile.read().find('my particular string')

Cheers,

- harold -

--
"All unsere Erfindungen sind nichts als verbesserte Mittel
  zu einem nicht verbesserten Zweck."
-- H.D. Thoreau




More information about the Python-list mailing list