Equivalent string.find method for a list of strings

jeremit0 jeremit0 at gmail.com
Tue Apr 12 20:13:15 EDT 2005


jeremit0 wrote:
> 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?
> Thanks,
> Jeremy
> 
After all the many suggestions (there were many good ones) I found 
something even better in "Python in a Nutshell".  I should have found it 
earlier.  I can do the following:

a = lines.index('my favorite line\n')

and a is the index of lines where "my favorite line' is located.  I 
hadn't seen this earlier, but it suits my purposes perfectly.
Thanks for everyone's suggestions,
Jeremy




More information about the Python-list mailing list