0
此代码每次在正在搜索的文件中输出一次匹配的字符串(所以我如果字符串重复出现,最终会得到一个巨大的列表)。我只想知道我的列表中的字符串是否匹配,而不是匹配多少次。我确实想知道哪些字符串匹配,所以True/False解决方案不起作用。但我只希望他们每次都列出一次,如果他们匹配的话。我不太了解pattern ='|'.join(关键字)部分在做什么 - 我从别人的代码中获得了该文件以使文件匹配工作,但不知道是否需要它。您的帮助将不胜感激。Python:如果找到一个字符串,停止搜索该字符串,搜索下一个字符串,并输出匹配的字符串
# declares the files used
filenames = ['//Katie/Users/kitka/Documents/appreport.txt', '//Dallin/Users/dallin/Documents/appreport.txt' ,
'//Aidan/Users/aidan/Documents/appreport.txt']
# parses each file
for filename in filenames:
# imports the necessary libraries
import os, time, re, smtplib
from stat import * # ST_SIZE etc
# finds the time the file was last modified and error checks
try:
st = os.stat(filename)
except IOError:
print("failed to get information about", filename)
else:
# creates a list of words to search for
keywords = ['LoL', 'javaw']
pattern = '|'.join(keywords)
# searches the file for the strings in the list, sorts them and returns results
results = []
with open(filename, 'r') as f:
for line in f:
matches = re.findall(pattern, line)
if matches:
results.append((line, len(matches)))
results = sorted(results)
# appends results to the archive file
with open("GameReport.txt", "a") as f:
for line in results:
f.write(filename + '\n')
f.write(time.asctime(time.localtime(st[ST_MTIME])) + '\n')
f.write(str(line)+ '\n')
我确实希望它能告诉我哪个文件的关键字被发现,所以谢谢。这肯定比我的原始代码更有效,并且它似乎工作。我会做更多的测试。谢谢! – Stefany