I am trying to extract numbers from a log file that i get from a serial port using puTTY. The log file updates itself every second and I want to parse the new line of the log file every time it updates itself.
I am doing this on python. Currently I have managed to parse through static log files and save the numbers that I want into a new file but am not sure how to go about having the files to continuously add new data that comes from the updated log.
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()
I expect the new log file to be continuously updated