这是我的代码:
import datetime
t1 = "Fri 11 Feb 2078 00:05:21"
t2 = "Mon 29 Dec 2064 03:33:48"
t3 = "Sat 02 May 2015 19:54:36"
t3_time = datetime.datetime.strptime(t3,"%a %d %B %Y %H:%M:%S")
print("debug")
t2_time = datetime.datetime.strptime(t2,"%a %d %B %Y %H:%M:%S")
t1_time = datetime.datetime.strptime(t1,"%a %d %B %Y %H:%M:%S")
错误: ValueError :时间数据“ 2064年12月29日星期一”与格式“%a%d%B%Y%H:%M:%S”不匹配
为什么 t3 得到正确的解析,而 t1 和 t2 没有得到正确的解析?
答案 0 :(得分:1)
您需要在输入中提供月份的全名
这应该有效
import datetime
t1 = "Fri 11 February 2078 00:05:21"
t2 = "Mon 29 December 2064 03:33:48"
t3 = "Sat 02 May 2015 19:54:36"
print("debug")
t1_time = datetime.datetime.strptime(t1,"%a %d %B %Y %H:%M:%S")
t2_time = datetime.datetime.strptime(t2,"%a %d %B %Y %H:%M:%S")
t3_time = datetime.datetime.strptime(t3,"%a %d %B %Y %H:%M:%S")
或者只使用%b代替%B
import datetime
t1 = "Fri 11 Feb 2078 00:05:21"
t2 = "Mon 29 Dec 2064 03:33:48"
t3 = "Sat 02 May 2015 19:54:36"
print("debug")
t1_time = datetime.datetime.strptime(t1,"%a %d %b %Y %H:%M:%S")
t2_time = datetime.datetime.strptime(t2,"%a %d %b %Y %H:%M:%S")
t3_time = datetime.datetime.strptime(t3,"%a %d %b %Y %H:%M:%S")
请注意,在输入中添加文本日期不会改变任何内容
例如
t4 = "Fri 02 May 2015 19:54:36"
t4_time = datetime.datetime.strptime(t4,"%a %d %B %Y %H:%M:%S")
print(t3_time == t4_time)
应返回True