How to get timedelta in seconds from a string? Example: +414 00:45:41.004000

I have data of timedeltas which looks like this:

time_delta = '+414 00:45:41.004000'

So, these values are strings and they are of the format ddd hh:mm:ss.f. I now want to get this deltas to seconds. I tried to use .total_seconds() but it did not work.

How could I achieve what I am trying to do?

Answer

If you always assume the same input format, you can build a function as below (result to be checked with a simple case) :

import datetime as dt

def parseTimeDelta(time_delta_str):
     splitted = time_delta_str.split(' ')
     day_part = int(splitted[0][1:])
     time_part = dt.datetime.strptime(splitted[1], "%H:%M:%S.%f")
     delta = dt.timedelta(days=day_part, hours=time_part.hour, minutes=time_part.minute, seconds=time_part.second,microseconds=time_part.microsecond)
     return delta.total_seconds()

time_delta = '+414 00:45:41.004000'
parseTimeDelta(time_delta)