B
B
Balundula2021-11-23 23:20:17
Python
Balundula, 2021-11-23 23:20:17

How to parse logs per minute?

There is an events.txt file like:

[2018-05-17 01:55:52.665804] NOK
[2018-05-17 01:56:23.665804] OK
[2018-05-17 01:56:55.665804] OK
[2018-05-17 01:57:16.665804] NOK
[2018-05-17 01:57:58.665804] OK
...

You need to parse NOK logs every minute, like:
[2018-05-17 01:57]
[2018-05-17 01:58]


Here is the code that is, but it does not work correctly
file_name = 'events.txt'
minutes = 38
with open(file=file_name, mode='r', encoding='utf8') as file:
    for line in file:
        if 'NOK' in line:
            if minutes == int(line[15:17]):
                print(line)
                minutes += 1

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question