Python Time conversion h:m:s to seconds

后端 未结 7 1229
青春惊慌失措
青春惊慌失措 2021-01-02 11:32

I am aware that with the timedelta function you can convert seconds to h:m:s using something like:

>> import datetime
>> str(datetime.timedelta(s         


        
7条回答
  •  星月不相逢
    2021-01-02 11:57

    Unfortunately, it's not as trivial as constructing a datetime object from a string using datetime.strptime. This question has been asked previously on Stack Overflow here: How to construct a timedelta object from a simple string , where the solution involved using python-dateutil.

    Alternatively, if you don't want to have to add another module, here is a class you can use to parse a timedelta from a string: http://kbyanc.blogspot.ca/2007/08/python-reconstructing-timedeltas-from.html

提交回复
热议问题