Reputation: 127
I have one file which is a list of phrases, one phrase on each line. The other file is not delimitated in any way, it's just one huge text file of words. I want to search for the phrases in the second file and if they are found, to print the phrase. This is the code I have so far.
f = open("phrase.txt", "r")
g = open("text.txt", "r")
for line in f:
search=line.lower()
for word in g:
if search in word:
print(search)
This is not printing anything for me, though.
EDIT: I changed the code to this:
f = open('phrase.txt').readlines()
f = [f.strip('\n').lower() for f in f]
g = open('text.txt').read()
for phrase in f:
if phrase in g:
print (phrase)
now i get the phrases that match. however some of the phrases have dashes (-) and more letters after them and they are not picked up by the program even if the phrase before the dash is present in text.txt. any way to change this?
Upvotes: 1
Views: 1985
Reputation: 1126
If you want to search for every phrase in the file, you would have to nest the loops, currently, you are just searching for the last phrase
phrases = open("phrase.txt").readLines()
for phrase in phrases:
search= phrase.lower()
words = open("text.txt", "r")
for word in words:
if search in word:
print(search)
words.close()
However, now things start to look funny, because you are asking if a phrase is in a word, which doesn't seem right. So
phrases = open("phrase.txt").readLines()
words = open("text.txt").read()
for phrase in phrases:
all_words_found = True
phrase_words = phrase.lower().split(" ")
for word in phrase_words:
if word not in words:
all_words_found = False
break
if all_words_found:
print phrase
This is what you want I do believe
Upvotes: 1
Reputation: 1948
f = open('phrase.txt').readlines()
f = [f.strip('\n').lower() for f in f]
g = open('text.txt').read()
words = g.split()
for phrase in f:
search_words = phrase.split()
for word in search_words:
if word in words:
print phrase
Upvotes: 0