Python 如何从每秒实时更新的日志文件中解析数据?
我试图从一个日志文件中提取数字,该文件是使用puTTY从串行端口获取的。日志文件每秒更新一次,我想在每次更新日志文件时解析日志文件的新行 我在python上做这个。目前,我已设法解析静态日志文件,并将所需的数字保存到新文件中,但不确定如何让这些文件不断添加来自更新日志的新数据Python 如何从每秒实时更新的日志文件中解析数据?,python,parsing,logging,real-time,Python,Parsing,Logging,Real Time,我试图从一个日志文件中提取数字,该文件是使用puTTY从串行端口获取的。日志文件每秒更新一次,我想在每次更新日志文件时解析日志文件的新行 我在python上做这个。目前,我已设法解析静态日志文件,并将所需的数字保存到新文件中,但不确定如何让这些文件不断添加来自更新日志的新数据 import re import time from time import strftime def main(): log_file_path = r"/Users/Mookuloo/Downloads/pu
import re
import time
from time import strftime
def main():
log_file_path = r"/Users/Mookuloo/Downloads/putty.log"
export_file_path = r"/Users/Mookuloo/Downloads\filtered"
time_now = str(strftime("%Y-%m-%d %H-%M-%S", time.localtime()))
file = "\\" + "Parser Output " + time_now + ".txt"
export_file = export_file_path + file
regex ='(SPHB=\d\d\d\D\d)'
parseData(log_file_path, export_file, regex, read_line=True)
def parseData(log_file_path, export_file, regex, read_line=True):
with open(log_file_path, "r") as file:
match_list = []
if read_line == True:
for line in file:
for match in re.finditer(regex, line, re.S):
match_text = match.group()
match_list.append(match_text)
print(match_text)
else:
data = file.read()
for match in re.finditer(regex, data, re.S):
match_text = match.group();
match_list.append(match_text)
file.close
with open(export_file, "w+") as file:
file.write("EXPORTED:\n")
match_list_clean = list(match_list)
for item in range(0, len(match_list_clean)):
number= float(match_list_clean[item][5:])
print (number)
file.write(str(number) + "\n")
file.close
if __name__ == '__main__':
main()
我希望新的日志文件会不断更新可能重复的