简体   繁体   English

如何使用关键字从txt文件搜索和检索整行

[英]How to search and retrieve whole lines from a txt file using Keyword

searchfile =open('test.txt','r')
    for line in searchfile:
        if line in array: print line
    searchfile.close() 

The search works except i have a keywords.txt document containing simple words like 'green, blue etc' (all on their own line) I then have a document with text such as 'my shirt is green' when i use this code it wont find anything but if I change the sentence in the txt file to just one word it will find it. 搜索工作正常,但我有一个keyword.txt文档,其中包含简单的单词,例如“绿色,蓝色等”(都在自己的行上),当我使用此代码时,我便有了一个文本例如“我的衬衫是绿色”的文档,找到任何东西,但是如果我将txt文件中的句子更改为一个单词,它将找到它。 I need it to search a document for keywords and then display the whole line that it was in. 我需要它在文档中搜索关键字,然后显示它所在的整行。

searchfile = open('keywords.txt', 'r')
infile = open('text.txt', 'r')

for keywords in searchfile:
    for lines in infile:
        if keywords in lines:
           print lines

Try this 尝试这个

searchfile = None
with open('test.txt','r') as f:
    searchfile = f.readlines()
    f.close()

for line in searchfile:
    for word in array:
        if word in line:
            print line

You can try this: 您可以尝试以下方法:

searchFile = open('keywords.txt','r')
file = open('text.txt','r') 
file1 = file.readlines()  
file.close()
for key in searchFile:
    for line in file1:
        if key in Line:
             print (line)

Make keywords a set , the check if any word in the line is in the set: 将关键字set ,检查该行中是否有任何单词:

with open('search.txt','r') as f1, open("keywords.txt") as f2:
    st = set(map(str.rstrip, f2))
    for line in f1:
        if any(word in st for word in  line.split()):
            print(line)

If you don't split "green" in 'my shirt is greenish' -> True . 如果您未"green" in 'my shirt is greenish' -> True拆分"green" in 'my shirt is greenish' -> True You also have to take into account punctuation and case. 您还必须考虑标点和大小写。

If you want to ignore case and remove punctuation, you can use str.lower and str.strip : 如果要忽略大小写并删除标点符号,可以使用str.lowerstr.strip

from string import punctuation
with open('search.txt','r') as f1, open("keywords.txt") as f2:
    st = set(map(str.rstrip, f2))
    for line in f1:
        if any(word.lower().strip(punctuation) in st for word in line.split()):
            print(line)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM