如何近似时间戳

How to approximate timestamp

我有一个以毫秒为单位的时间戳列表,例如 10:35:08.23562515422。现在以毫秒为单位,我只想在列表中包含前 3 个值,例如 10:35:08:235。有办法吗?

>>> from datetime import datetime

>>> timestamps = ['10:35:08.23562515422', '10:35:08.24862517422', '10:35:08.31060347422', '10:35:15.00860347422']

>>> for time in timestamps:
...     dt = datetime.strptime(time[:-8], '%H:%M:%S.%f')
...     print datetime.strftime(dt, '%H:%M:%S.%f')[:-3]

10:35:08.235
10:35:08.248
10:35:08.310
10:35:15.008

假设小时、分钟和秒总是两位数(它们应该是),它很简单:

timestamp = timestamp[:12]