gpt4 book ai didi

python - 值错误 : time data '22.12.2012 17:00' does not match format '%d.%m.%Y %I:%M' , 上午/下午

转载 作者:行者123 更新时间:2023-12-01 05:51:37 24 4
gpt4 key购买 nike

在 python 中将字符串解析为日期时间对象时遇到一个小问题。以下代码适用于某些值,但并不总是适用,并且不知道这种简单的代码和平有什么问题。

datetime.datetime.strptime("22.12.2012 17:00", '%d.%m.%Y %I:%M')

ValueError: time data '22.12.2012 17:00' does not match format '%d.%m.%Y %I:%M'

我认为问题可能与时间和上午/下午有关?因为错误仅在时间 > 12:00 时弹出,并且字符串“22.12.2012 17:00”工作正常。

感谢帮助

最佳答案

尝试使用 %H token 而不是 %I:

import datetime
datetime.datetime.strptime("22.12.2012 17:00", '%d.%m.%Y %H:%M')

%I 表示 12 小时日期格式。 %H 用于 24 小时日期格式,如文档中所述:http://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior

关于python - 值错误 : time data '22.12.2012 17:00' does not match format '%d.%m.%Y %I:%M' , 上午/下午,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14043552/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com