时间数据“2021年5月10日”与格式“%m%d%Y”不匹配

我在打印由字符串生成的格式化时间对象时遇到问题

这是我的代码:

date_time_str = 'May 10 2021'
date_time_obj = datetime. strptime(date_time_str, '%m %d %Y')
print("The type of the date is now", type(date_time_obj))

This is the error:
ValueError: time data 'May 10 2021' does not match format '%m %d %Y'

回答

根据此链接,Month格式中的一个月,您需要使用%BMth格式 ('Apr','Jun') 中的一个月,使用%b.

您使用的是%m,它用于数字。

下面是一个例子:

import time
import datetime
from time import strptime
print("hello world")
date_time_str = 'May 10 2021'
date_time_obj = strptime(date_time_str, '%B %d %Y')
print("The type of the date is now", date_time_obj)


以上是时间数据“2021年5月10日”与格式“%m%d%Y”不匹配的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>