Nov-20-2023, 08:43 PM
(This post was last modified: Nov-20-2023, 08:43 PM by deanhystad.)
It is possible with pandas, but pandas doesn't provide much help with this task. The format of the log file also makes it difficult for pandas to parse easily.
You should include the date when getting the times so periods spanning the end of day compute the correct delta. The log should also include year.
You should include the date when getting the times so periods spanning the end of day compute the correct delta. The log should also include year.
from io import StringIO from datetime import datetime log = StringIO( """<150>Nov 7 07:38:33 DrayTek: Local User (MAC=B0-8C-75-C0-FF-8F): 172.16.91.7:57422 -> 17.57.172.11:443 (TCP) <150>Nov 7 07:38:43 DrayTek: Local User (MAC=B0-8C-75-C0-FF-8F): 172.16.91.7:57423 -> 17.57.172.11:443 (TCP) <150>Nov 7 08:38:33 DrayTek: Local User (MAC=B0-8C-75-C0-FF-8F): 172.16.91.7:57424 -> 17.57.13.65:443 (TCP) <150>Nov 8 07:38:33 DrayTek: Local User (MAC=B0-8C-75-C0-FF-8F): 172.16.91.7:57425 -> 17.57.13.65:443 (TCP)""" ) prev = None for line in log: dt = datetime.strptime(line[5:20], "%b %d %H:%M:%S") if prev is not None: print(dt - prev) prev = dt
Output:0:00:10
0:59:50
23:00:00