Equivalent string.find method for a list of strings
Jack Diederich
jack at performancedrivers.com
Fri Apr 8 15:46:35 EDT 2005
On Fri, Apr 08, 2005 at 02:17:32PM -0400, 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?
def search(line):
return 'my string' in line
lines = filter(search, myfile.readlines())
if you are using python2.4, use a generator expression so it won't make
a list the size of the whole (possibly large?) file.
for (matching_line) in (line for (line) in myfile if ('my string' in line)):
# do something
-jackdied
More information about the Python-list
mailing list