The first step is to parse all those timestamps into something that you can perform arithmetic on. This can be timedelta
objects, or integer microseconds (or milliseconds, since your times all have 0 micros), or float seconds, or any other reasonable type.
For example, assuming that input is one big string:
ts = []
for h, m, s, u in re.findall(r'(\d+):(\d+):(\d+)(?:\.(\d+))?', bigstring):
h, m, s = int(h), int(m), int(s)
u = int(u) if u else 0
ts.append(datetime.timedelta(hours=h, minutes=m, seconds=s, microseconds=u))
If it's a list of strings, or a file object, etc. just change it to iterate that and do re.search
on each one, instead of iterating re.findall
.
Then we can average them the same as any other values:
sum(ts, datetime.timedelta()) / len(ts)
Since I used timedelta
values, that's what the result will be:
datetime.timedelta(0, 0, 659333)
… or, if you print
it:
0:00:00.659333
… or, if you want it as, say, a number of seconds, just call its total_seconds()
method:
0.659333