我是python的新手,im正在构建一个关键字搜索程序,该程序应该在一个larg.log文件(目前是单词“时间戳”)中找到所有相同的单词,并将值存储在单独的.log文件中。在
这是我写的代码,我的问题是,我只得到第一次“时间戳”弹出在.log文本新文件,而不是+100次,我想它。。。在
帮忙吗?:)
def search(文件名,文本):
重要=[]with open(filename) as f: #open search file
f = f.readlines() #read searrch file
for lines in f:
if text in lines: #if keyword is found take all lines
important.append(lines) #store all found keywords
print('done')
break
# file or 'sorted.txt'
with open('sorted.log', 'w') as file_handler:
file_handler.write(f"{filename}\n Status: {important} \n")
search(r'C:\Users\AppData\Local\Programs\Python\search\OJW74.log', 'Time stamp:') # search path
***例如.(.log文件锁定如下)
^{pr2}$