【问题标题】:Convert string to date python strptime error将字符串转换为日期python strptime错误
【发布时间】:2018-05-23 19:42:19
【问题描述】:

我尝试将此日期字符串:Wed May 23 15:45:43 +0000 2018 转换为带有 strptime 的 python 日期对象。 我检查了我应该如何使用它,但我绝对不明白我的问题出在哪里。

我刚试过这个

date = datetime.strptime('Wed May 23 15:45:43 +0000 2018', '%a %b %d %x %z %Y')

它应该是正确的格式,但我收到了这个错误:

sre_constants.error: redefinition of group name 'd' as group 5; was group 3 at position 169

这是否意味着 23 不是 %d 格式?

感谢您的帮助

【问题讨论】:

    标签: python-3.x strptime


    【解决方案1】:

    您的格式与日期字符串不匹配。要了解有关格式的更多信息,请参阅此https://docs.python.org/2/library/datetime.html。 我已经为您的代码制作了正确的格式。这是工作代码

    from datetime import datetime
    date = datetime.strptime('Wed May 23 15:45:43 +0000 2018',
                             '%a %b %d %H:%M:%S %z %Y')
    print(date) # Output 2018-05-23 15:45:43+00:00
    

    工作代码在这里https://ideone.com/ONDSyD

    【讨论】:

    • 好的,感谢您的帮助。在那种情况下 %X 的用途是什么?
    猜你喜欢
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-11
    • 2013-06-28
    • 1970-01-01
    • 2018-05-22
    相关资源
    最近更新 更多