将文本文件中找到的关键字从关键字列表返回到新文件?
Returning keywords found in a text file from a keyword list to a new file?
简介:
我目前正在构建一个关键字检测程序。给定一些“.txt”文件并循环遍历它们,从关键字列表中搜索其中的关键字,返回哪些文件包含该关键字。关键字存储在单独的 python 文件中的列表中,然后导入到主程序文件中。
目标:
我想实现的目标是在解析文本文件时打印出从列表中找到的关键字。因此,例如,当它搜索文本文件并且“Hello”在关键字列表中时,我希望输出为“Hello,在 example_text01.txt 中找到”。目前它只是 returns 是否找到关键字。理想情况下,该过程应如下所示。
示例单词列表:
word_list = ["Demo", "Text", "Hello", "Example"]
示例文本:
Hello how are you?
期望的结果:
"Hello, found in example_text01.txt"
我尝试过的:
- 尝试使用
in
关键字。
运行 没有错误,但它会跳过任何带有关键字的文本文件而不处理它。
- 将关键字文件设为纯文本并使用
readline()
解析文本。
收到以下错误:AttributeError: 'list' object has no attribute 'readlines'
- 写结果文档时返回
keyword
class
刚回来<class 'ast.keyword'>
代码:
以下是我目前使用的代码。
keywords = ['Hello', 'Example', 'Keywords']
# Create and open result.txt where results of keyword scan will be stored
with open("/PATH/TO/result.txt", "w") as f:
#Path to the folder the .txt files are stored in within the loop
for filename in listdir("/PATH/TO/txt"):
# Opens all text files as they are processed through the loop
with open('/PATH/TO/CURRENT/TEXT/FILE/IN/txt/example.txt') as currentFile:
text = currentFile.read()
if any(keyword in text for keyword in keywords):
f.write('Keyword found in ' + filename[:-4] + '\n')
else:
f.write('No keyword in ' + filename[:-4] + '\n')
代码的当前输出是,如果在其中一个文本文件中找到关键字列表中的关键字,那么无论是否找到关键字,程序都将写入 'results.txt' 文件。但是,与此同时,我想找到一种方法来包含找到的关键字。任何帮助将不胜感激,谢谢!
为什么不只修改底部:
而不是
if any(keyword in text for keyword in keywords):
f.write('Keyword found in ' + filename[:-4] + '\n')
else:
f.write('No keyword in ' + filename[:-4] + '\n')
...
for k in keywords:
f.write((f'Keyword "{k}" found in ' if keyword in text else 'No keyword in ') + filename[:-4] + '\n')
只需更改:
if any(keyword in text for keyword in keywords):
f.write('Keyword found in ' + filename[:-4] + '\n')
else:
f.write('No keyword in ' + filename[:-4] + '\n')
至:
keywordsFound = [k for k in keywords if k in text] #get all found keywords
if keywordsFound: #if keywords were found
for k in keywordsFound:#for each found keyword
f.write(f'{k}, found in {filename[:-4]}\n') #say it was found
else:
f.write(f'No keyword in {filename[:-4]}\n') #if non-found say it was not found
这会获取在文件中找到的每个关键字,然后写入另一个文件。
如果您只想要找到的第一个关键字,您可以使用:
keywordsFound = [k for k in keywords if k in text] #get all found keywords
if keywordsFound: #if keywords were found
k = keywordsFound[0] #get only first keyword
f.write(f'{k}, found in {filename[:-4]}\n') #say it was found
else:
f.write(f'No keyword in {filename[:-4]}\n') #if non-found say it was not found
简介:
我目前正在构建一个关键字检测程序。给定一些“.txt”文件并循环遍历它们,从关键字列表中搜索其中的关键字,返回哪些文件包含该关键字。关键字存储在单独的 python 文件中的列表中,然后导入到主程序文件中。
目标:
我想实现的目标是在解析文本文件时打印出从列表中找到的关键字。因此,例如,当它搜索文本文件并且“Hello”在关键字列表中时,我希望输出为“Hello,在 example_text01.txt 中找到”。目前它只是 returns 是否找到关键字。理想情况下,该过程应如下所示。
示例单词列表:
word_list = ["Demo", "Text", "Hello", "Example"]
示例文本:
Hello how are you?
期望的结果:
"Hello, found in example_text01.txt"
我尝试过的:
- 尝试使用
in
关键字。
运行 没有错误,但它会跳过任何带有关键字的文本文件而不处理它。
- 将关键字文件设为纯文本并使用
readline()
解析文本。
收到以下错误:AttributeError: 'list' object has no attribute 'readlines'
- 写结果文档时返回
keyword
class
刚回来<class 'ast.keyword'>
代码:
以下是我目前使用的代码。
keywords = ['Hello', 'Example', 'Keywords']
# Create and open result.txt where results of keyword scan will be stored
with open("/PATH/TO/result.txt", "w") as f:
#Path to the folder the .txt files are stored in within the loop
for filename in listdir("/PATH/TO/txt"):
# Opens all text files as they are processed through the loop
with open('/PATH/TO/CURRENT/TEXT/FILE/IN/txt/example.txt') as currentFile:
text = currentFile.read()
if any(keyword in text for keyword in keywords):
f.write('Keyword found in ' + filename[:-4] + '\n')
else:
f.write('No keyword in ' + filename[:-4] + '\n')
代码的当前输出是,如果在其中一个文本文件中找到关键字列表中的关键字,那么无论是否找到关键字,程序都将写入 'results.txt' 文件。但是,与此同时,我想找到一种方法来包含找到的关键字。任何帮助将不胜感激,谢谢!
为什么不只修改底部:
而不是
if any(keyword in text for keyword in keywords):
f.write('Keyword found in ' + filename[:-4] + '\n')
else:
f.write('No keyword in ' + filename[:-4] + '\n')
...
for k in keywords:
f.write((f'Keyword "{k}" found in ' if keyword in text else 'No keyword in ') + filename[:-4] + '\n')
只需更改:
if any(keyword in text for keyword in keywords):
f.write('Keyword found in ' + filename[:-4] + '\n')
else:
f.write('No keyword in ' + filename[:-4] + '\n')
至:
keywordsFound = [k for k in keywords if k in text] #get all found keywords
if keywordsFound: #if keywords were found
for k in keywordsFound:#for each found keyword
f.write(f'{k}, found in {filename[:-4]}\n') #say it was found
else:
f.write(f'No keyword in {filename[:-4]}\n') #if non-found say it was not found
这会获取在文件中找到的每个关键字,然后写入另一个文件。
如果您只想要找到的第一个关键字,您可以使用:
keywordsFound = [k for k in keywords if k in text] #get all found keywords
if keywordsFound: #if keywords were found
k = keywordsFound[0] #get only first keyword
f.write(f'{k}, found in {filename[:-4]}\n') #say it was found
else:
f.write(f'No keyword in {filename[:-4]}\n') #if non-found say it was not found