top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Searching for a list of strings in a file with Python

+2 votes
482 views

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:

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?

posted Oct 14, 2013 by Luv Kumar

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

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
answer Oct 14, 2013 by Sonu Jindal
+1 vote

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.

answer Oct 14, 2013 by Amit Parthsarthi
Similar Questions
+2 votes

I have a multi-line string and I need to remove the very first line from it. How can I do that? I looked at StringIO but I can't seem to figure out how to properly use it to remove the first line.

+1 vote

Here is something I found curious about python loops.

This loop run each character in a string

def avoids(word,letters):
 flag = True
 for letter in letters:
 if(letter in word):
 flag = False
 return flag

The loop below (at the bottom) runs each line of the file

fin = open('wordplay.txt');
user_input = raw_input('Enter some characters: ')
count = 0
for line in fin:
 word = line.strip()
 if(avoids(word, user_input)):
 count += 1;

This is just too convenient. Basically my question is: Why is python not treating the contents of wordplay.txt as one long string and looping each character?

Any comment is greatly appreciate.

...