克服Python 3 datetime 中strptime 解析方法中%f 的6 位数字限制

Overcoming the 6 digits limitation of %f in strptime parsing method in Python 3 datetime

似乎 datetime class 的 strptime 函数在使用 %f (https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior)

解析小数秒时被限制为 6 位数字

只是想知道是否有 "decent" 解决方法,除了我自己编写正则表达式只是为了实现与已经为多一个数字实现的几乎相同的事情。

此处已报告相同内容:https://github.com/getsentry/sentry/issues/1610

如果你有一个固定的输入格式,你可以把剩下的位去掉:

>>> s = '2015-07-15T11:39:37.0341297Z'
>>> s[:26]
'2015-07-15T11:39:37.034129'
>>> datetime.strptime(s[:26], '%Y-%m-%dT%H:%M:%S.%f')
datetime.datetime(2015, 7, 15, 11, 39, 37, 34129)