Personally, I would not use regex in situations where the position of the data is known and unchanging; rather, I'd simply use the index positions.
As a simple demonstration:
As a simple demonstration:
with open("log", mode="r", encoding="UTF-8") as log: for entry, item in enumerate(log, 1): time_stamp = item[12:20] print(f"{entry:02d}:~", time_stamp)To add: as working demonstration of the difference between the time entries:
from datetime import datetime time_stack = [] with open("log", mode="r", encoding="UTF-8") as log: for entry, item in enumerate(log, 1): time_str = item[12:20] time_stamp = datetime.strptime(time_str, '%H:%M:%S') time_stack.append(time_stamp) if len(time_stack) > 1: print(f"{entry:02d}:~", time_str, (time_stack[1] - time_stack[0])) time_stack.pop(0) else: print(f"{entry:02d}:~ Start", time_str)
Output:01:~ Start 07:38:33
02:~ 07:38:33 0:00:00
03:~ 07:38:33 0:00:00
04:~ 07:38:33 0:00:00
05:~ 07:38:33 0:00:00
06:~ 07:38:33 0:00:00
07:~ 07:38:33 0:00:00
08:~ 07:38:33 0:00:00
09:~ 07:38:33 0:00:00
10:~ 07:38:33 0:00:00
11:~ 07:38:33 0:00:00
12:~ 07:38:33 0:00:00
13:~ 07:38:34 0:00:01
14:~ 07:38:34 0:00:00
15:~ 07:38:35 0:00:01
16:~ 07:38:37 0:00:02
Sig:
>>> import this
The UNIX philosophy: "Do one thing, and do it well."
"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse
"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
>>> import this
The UNIX philosophy: "Do one thing, and do it well."
"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse
"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein