[英]time data '2022-01-07T02:05:1' does not match fotmat '%Y-%m-%d%H:%M:%S'
I have some unpredicted errors with a date string.我的日期字符串有一些无法预料的错误。 I have a try/except to navigate through it at it worked for a while, but now I'm running into this.我有一个尝试/除了浏览它,它工作了一段时间,但现在我遇到了这个。
Here is my current code:这是我当前的代码:
def parse_date(date_string):
try:
return datetime.datetime.strptime(date_string, "%Y-%m-%dT%H:%M:%SZ")
except ValueError:
return datetime.datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
I the except I tried to have another try/except for certain format to account for lack of zero-padding, such as this:我除了尝试对某些格式进行另一种尝试/排除以解决缺少零填充的问题,例如:
def parse_date(date_string):
try:
return datetime.datetime.strptime(date_string, "%Y-%m-%dT%H:%M:%SZ")
except ValueError:
try:
return datetime.datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
except:
return datetime.datetime.strptime(date_string, "%Y-%m-%d %H:%M:%-S")
No luck so far.到目前为止没有运气。 Any suggestion where i could go from here?有什么建议我可以从这里 go 吗?
You just have to remove the Z at the end and it should work.你只需要在最后删除 Z 就可以了。
def parse_date(date_string):
try:
return datetime.datetime.strptime(date_string, "%Y-%m-%dT%H:%M:%S")
except ValueError:
return datetime.datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.