1

我有一个包含 200 个单词的文件,每个单词都换行。我想在另一个文件中搜索所有这些单词。我希望打印包含这些单词之一的每个句子。现在,只出现第一个单词的匹配项。之后,它停止。

corpus = open('C:\\Users\\Lucas\\Desktop\\HAIT\\Scriptie\\Tweet-corpus\\Corpus.txt', 'r', encoding='utf8')

with open('C:\\Users\\Lucas\\Desktop\\HAIT\\Scriptie\\Tweet-corpus\\MostCommon3.txt', 'r', encoding='utf8') as list:
for line in list:
    for a in corpus:
        if line in a:
            print(a)
4

1 回答 1

3
# Prepare the list of words
word_file = open('wordfile', 'r', encoding='utf8')
words = [word.strip() for word in word_file.readlines()]
word_file.close()

# Now examine each sentence:
with open('sentencefile') as sentences:
    for sentence in sentences:
        found = False
        for word in words:
            if word in sentence:
                found = True
                break
        if found:
            print sentence
于 2013-10-04T16:12:21.987 回答