Find phrases from one text file in another text file with python -
i have 1 file list of phrases, 1 phrase on each line. other file not delimitated in way, it's 1 huge text file of words. want search phrases in second file , if found, print phrase. code have far.
f = open("phrase.txt", "r") g = open("text.txt", "r") line in f: search=line.lower() word in g: if search in word: print(search)
this not printing me, though.
edit: changed code this:
f = open('phrase.txt').readlines() f = [f.strip('\n').lower() f in f] g = open('text.txt').read() phrase in f: if phrase in g: print (phrase)
now phrases match. of phrases have dashes (-) , more letters after them , not picked program if phrase before dash present in text.txt. way change this?
if want search every phrase in file, have nest loops, currently, searching last phrase
phrases = open("phrase.txt").readlines() phrase in phrases: search= phrase.lower() words = open("text.txt", "r") word in words: if search in word: print(search) words.close()
however, things start funny, because asking if phrase in word, doesn't seem right. so
phrases = open("phrase.txt").readlines() words = open("text.txt").read() phrase in phrases: all_words_found = true phrase_words = phrase.lower().split(" ") word in phrase_words: if word not in words: all_words_found = false break if all_words_found: print phrase
this want believe
Comments
Post a Comment