简体   繁体   English

时间数据“2022-01-07T02:05:1”与格式“%Y-%m-%d%H:%M:%S”不匹配

[英]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.

相关问题 ValueError: 时间数据 '2013/05/24 07:00:00' 与格式 '%Y-%m-%d %H:%M:%S' 不匹配 - ValueError: time data '2013/05/24 07:00:00' does not match format '%Y-%m-%d %H:%M:%S' 值错误:时间数据'2019-03-19T07:01:02Z'与格式'%Y-%m-%dT%H:%M:%S.%fZ'不匹配 - Value Error :Time data '2019-03-19T07:01:02Z' does not match format '%Y-%m-%dT%H:%M:%S.%fZ' Django:时间数据 '2022-09-02 11:13 am' 与格式 '%Y-%m-%d %H%M%S.%f' 不匹配 - Django: time data '2022-09-02 11:13 am' does not match format '%Y-%m-%d %H%M%S.%f' Python 时间数据 '2008-01-24T00:00:00:000' 与格式 '%Y-%m-%d %H:%M :%S:%f' 不匹配 - Python time data '2008-01-24T00:00:00:000' does not match format '%Y-%m-%d %H:%M :%S:%f' ValueError:时间数据“ 140120 1520”与格式“%Y-%m-%d%H:%M:%S”不匹配 - ValueError: time data '140120 1520' does not match format '%Y-%m-%d %H:%M:%S' ValueError: 时间数据与格式 '%Y-%m-%d %H:%M:%S.%f' 不匹配 - ValueError: time data does not match format '%Y-%m-%d %H:%M:%S.%f' 出现错误“ValueError:时间数据''与格式'%Y-%m-%d %H:%M:%S'不匹配” - Getting error "ValueError: time data '' does not match format '%Y-%m-%d %H:%M:%S'" ValueError: 时间数据“无”与格式“%Y-%m-%d %H:%M:%S”不匹配 - ValueError: time data 'None' does not match format '%Y-%m-%d %H:%M:%S' 时间数据与格式 '%Y-%m-%d %H:%M:%S' 不匹配 - time data does not match format '%Y-%m-%d %H:%M:%S' Python strptime:时间数据“ 2016-02-02”与格式“%Y-%m-%d”不匹配 - Python strptime : time data '“2016-02-02”' does not match format '%Y-%m-%d'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM