Questions & Answers

How to convert tuple to seconds?

I am running an sql query in python.


It gives me the result just fine.

<class 'tuple'> (datetime.timedelta(days=-1, seconds=86332),)

I want to convert this to be only seconds.

I tried to add the seconds as interval into my query but it cannot be run that way.

Select Timediff(second, ...)

I also tried DATEDIFF, not worked.

After I add the 'second' the query cannot be executed

So, I want to calculate in pyton the difference, if no other solution.

2023-01-17 00:49:09
Please update the description using backticks and clearer question will be better.
2023-01-17 00:49:09
get the first item of the tuple, and then use timedelta methods to get the seconds
2023-01-17 00:49:09
( - of course, in reality using different times
Answers(1) :

You can get the timedelta object from the tuple using indexing:

td = result[0]

then get the number of seconds using the timedelta object's total_seconds method.

seconds = td.total_seconds()