Hel*_*lmi 5 python time datetime timedelta
我相信一定有更好的方法将00:04:11.723像这样的字符串转换为微秒:
ms = timestring.split('.')
pt = ms[0].split(':')
sec = int(pt[0]) * 3600 + int(pt[1]) * 60 + int(pt[2])
st = sec * 1000 + int(ms[1])
Run Code Online (Sandbox Code Playgroud)
但我还没找到。
这是仅使用的一种方式datetime.timedelta:
from datetime import timedelta
x = '00:04:11.723'
h, m, s = map(float, x.split(':'))
res = timedelta(hours=h, minutes=m, seconds=s).total_seconds() * 1000
# 251723.0
Run Code Online (Sandbox Code Playgroud)
如果您不反对第三方库,pandas.Timedelta可能对您有用:
import pandas as pd
pd.Timedelta(timestring).total_seconds()*1000
Run Code Online (Sandbox Code Playgroud)
这将返回:251723.0
| 归档时间: |
|
| 查看次数: |
849 次 |
| 最近记录: |