字符串的日期时间不匹配

Ale*_*lex 3 python datetime

我试图匹配字符串中的特定日期时间格式,但我收到了一个ValueError,我不知道为什么.我使用以下格式:

t = datetime.datetime.strptime(t,"%b %d, %Y %H:%M:%S.%f Eastern Standard Time")
Run Code Online (Sandbox Code Playgroud)

这是尝试匹配以下字符串:

Nov 19, 2017 20:09:14.071360000 Eastern Standard Time
Run Code Online (Sandbox Code Playgroud)

任何人都可以看到为什么这些不匹配?

pau*_*ult 5

文档中我们可以看到%f预期:

Microsecond为十进制数,左侧为零填充.

你的字符串的问题是你有一个在右边填零的数字.

以下是解决问题的一种方法:

new_t = t.partition(" Eastern Standard Time")[0].rstrip('0') + ' Eastern Standard Time'
print(new_t)
#Nov 19, 2017 20:09:14.07136 Eastern Standard Time

t2 = datetime.datetime.strptime(new_t,"%b %d, %Y %H:%M:%S.%f Eastern Standard Time")
print(t2)
#datetime.datetime(2017, 11, 19, 20, 9, 14, 71360)
Run Code Online (Sandbox Code Playgroud)