Searching for a list of strings in a file with Python

Denis McMahon denismfmcmahon at gmail.com
Mon Oct 14 16:38:56 EDT 2013


On Sun, 13 Oct 2013 22:34:43 -0700, Starriol wrote:

> I'm trying to search for several strings, which I have in a .txt file
> line by line, on another file.
> So the idea is, take input.txt and search for each line in that file in
> another file, let's call it rules.txt.
> 
> So far, I've been able to do this, to search for individual strings:
> 
> [code]import re
> 
> shakes = open("output.csv", "r")
> 
> for line in shakes:
>     if re.match("STRING", line):
>         print line,[/code]
> 
> How can I change this to input the strings to be searched from another
> file?
> 
> So far I haven't been able to.

read all the search strings into a list

while there's data in shakes
     read a line from shakes
     for each string in search string list
         search the line from shakes for the search string

-- 
Denis McMahon, denismfmcmahon at gmail.com



More information about the Python-list mailing list