将 HH:MM:SS.micro 字符串转换为微秒?

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)

但我还没找到。

jpp*_*jpp 5

这是仅使用的一种方式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)


sac*_*cuL 3

如果您不反对第三方库,pandas.Timedelta可能对您有用:

import pandas as pd
pd.Timedelta(timestring).total_seconds()*1000
Run Code Online (Sandbox Code Playgroud)

这将返回:251723.0