Searching for a list of strings in a file with Python

S

Starriol

Hi guys,

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,

How can I change this to input the strings to be searched from another file?

So far I haven't been able to.

Thanks for the ideas.
 
P

Peter Otten

Starriol said:
Hi guys,

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,

How can I change this to input the strings to be searched from another
file?

Assuming every line in input.txt contains a regular expression and you don't
care which one matches:

shakes = open("output.csv")

# you will iterate over the regex multiple times,
# so put them in a list
rattles = [line.strip() for line in open("input.txt")]

for line in shakes:
for expr in rattles:
if re.match(expr, line):
print line,
break # out of the for-rattles loop
# at the first matching regex
 
D

Dave Angel

Hi guys,

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,

How can I change this to input the strings to be searched from another file?

So far I haven't been able to.

Thanks for the ideas.

Take your existing code, and make the bulk of it into a function
definition. The function should take the 'string' as a parameter.

Once you've got that debugged, you can write code that reads your
pattern file, and for each line of it, calls the function you just
wrote.

You can certainly do it the way Peter describes, but if you learn to
factor each problem into separate functions (or classes, or generator,
or decorator, or ...) then your code will be easier to test, easier to
understand, and easier to reuse.
 
D

Denis McMahon

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,

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top