There is such a code that counts data from the entire log file (the log file is constantly updated), and writes only the result of the sum of the last count into a variable.

import re import operator last = operator.itemgetter(-1) a = open('14.04.2019.log', 'r') while a: for i in a: res = re.findall(r': (\d+) \+ (\d+)$', i) if res: a_list = [(sum(map(int, *res)))] b = last(a_list) 

how to implement the print variable when it changes, that is, with each change, a number was output. And in the end it turned out for example

 123 6869 457 235 
  • If my answer helped you, mark it, please, as accepted by pressing v under the vote count - Sergey Nudnov

1 answer 1

What is written above you, unfortunately, will not work. a is a handle to an open file. To get the text of your log line by line, use a.readline()

Since in response to your previous question, I have already given a variant of the code, so we will finalize it:

  import re r_pattern = re.compile(': (\d+) \+ (\d+)$') # Предыдущее найденное число prev_value = None with open('test.log','rt',encoding='utf-8') as f: while True: line = f.readline() if not line: break match = re.search(r_pattern, line) if match: value = int(match.group(1)) + int(match.group(2)) if not value == prev_value: # Сравниваем с предыдущим prev_value = value # Обновляем предыдущее число на новое print(value) # И печатаем 

Just a couple of moments on your code:

 a_list = [] while ... for ... if res: a_list.append(sum(map(int, *res))) 

And why not?

 b = a_list[-1]