gpt4 book ai didi

python - Python 字符串转日期时间的问题

转载 作者:行者123 更新时间:2023-11-30 23:33:17 29 4
gpt4 key购买 nike

我在 Python 中有以下字符串 - 'Oct 01 2013 07 30 PM EST'。我正在尝试转换为日期时间对象,但收到以下错误:

ValueError: time data 'Oct 01 2013 07 30 PM EST' does not match format
'%b %d %Y %I %M %p %Z'

不知道哪里错了!请帮忙。我的代码如下:

from datetime import datetime
date_object = datetime.strptime(str(date), '%b %d %Y %I %M %p %Z')

最佳答案

Python strptime 与平台相关:

http://docs.python.org/2/library/time.html#time.strptime

Support for the %Z directive is based on the values contained in tzname and whether daylight is true. Because of this, it is platform-specific except for recognizing UTC and GMT which are always known (and are considered to be non-daylight savings timezones).

例如,在我的套件(OSX 10.8.5、python 2.7.2)上,datetime.now().strftime("%Z") 是一个空字符串。

有像http://pytz.sourceforge.net/这样的外部库帮助处理时区

关于python - Python 字符串转日期时间的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18948004/

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