gpt4 book ai didi

python - 有没有办法将不一致的字符串时间转换为 python 时间对象?

转载 作者:行者123 更新时间:2023-11-28 22:39:29 25 4
gpt4 key购买 nike

我有这样的时间戳。

12:30PM
11AM

我意识到我可以使用 strptime 来转换它。但是,如果某些字符串没有分钟,有没有办法在 Python 中自动检测到它?

最佳答案

python-dateutil可以提供帮助:

>>> from dateutil.parser import parse
>>> parse("12:30PM")
datetime.datetime(2016, 1, 10, 12, 30)
>>> parse("11AM")
datetime.datetime(2016, 1, 10, 11, 0)

关于python - 有没有办法将不一致的字符串时间转换为 python 时间对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34712324/

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