我有一个时间戳列表,其中包含10:35:08.23562515422
之类的毫秒数。现在在几毫秒内,我希望列表中只有前3个值,如10:35:08:235
。有办法吗?
答案 0 :(得分:0)
>>> 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
答案 1 :(得分:0)
假设小时,分钟和秒钟总是两位数(它们应该是),它就像以下一样简单:
timestamp = timestamp[:12]