将带小数的字符串转换为datetime

时间:2018-05-08 14:59:58

标签: python datetime time

这个问题与this SO非常相似,但我的数据在表格中:

'13-04-2018 14:06:26.866'
'13-04-2018 14:06:27.131'
'13-04-2018 14:06:27.404'
'13-04-2018 14:06:27.674'
...

即。秒以小数形式给出。我对datetime documentation的阅读表明它不支持这种格式,所以我不确定如何最好地继续。

1 个答案:

答案 0 :(得分:2)

看起来你需要

  • .%f == Microsecond as a decimal number, zero-padded on the left.

<强>实施例

import datetime
s = '13-04-2018 14:06:26.866'
print(datetime.datetime.strptime(s, "%d-%m-%Y %H:%M:%S.%f"))

<强>输出:

2018-04-13 14:06:26.866000
相关问题