【问题标题】:Python: How to convert string time to decimal seconds?Python:如何将字符串时间转换为十进制秒?
【发布时间】:2021-05-15 08:10:18
【问题描述】:
String Time 
00:51:21,920

Decimal Time
3,081.92 (second)

有没有可以以秒为单位在字符串时间和十进制时间之间转换的方法?我想在moviepy VideoFileClip.subclip中使用它。

以下是我所做的并且有效。但我认为应该有一种更简单的方法,比如库中的函数。

def TrsTime(VideoTime):
  return (datetime.strptime(VideoTime, '%H:%M:%S,%f').hour*60*60+
  datetime.strptime(VideoTime, '%H:%M:%S,%f').minute*60+
  datetime.strptime(VideoTime, '%H:%M:%S,%f').second+
  datetime.strptime(VideoTime, '%H:%M:%S,%f').microsecond/1000/1000)

【问题讨论】:

标签: python python-3.x string python-datetime


【解决方案1】:

是的,有一个更简单的方法:

from datetime import datetime as dt

(dt.strptime(VideoTime, '%H:%M:%S,%f') - dt(1900, 1, 1)).total_seconds()

>>> 3081.92

这适用于 timedelta 对象具有 .total_seconds() 函数的主体。因此,通过从 1900 年 1 月 1 日减去您的时间(其年、月、日默认为 1900 年 1 月 1 日),您将获得以秒为单位的增量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-07
    • 2020-06-20
    • 2011-08-25
    • 2014-04-28
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多