Python-exercises/parser_log.py

15 lines
No EOL
428 B
Python

import collections
logs = [ "2026-05-26 ERROR: connection timeout", "2026-05-26 INFO: server started", "2026-05-26 ERROR: connection timeout", "2026-05-26 WARN: high memory", "2026-05-26 ERROR: disk full", ]
words = logs.split('#', 1)
error = collections.Counter('ERROR')
num_error = error.total()
print(f"Error: {num_error}")
# {'ERROR': 3, 'INFO': 1, 'WARN': 1} top erros: [('connection timeout', 2), ('disk full', 1)]