将Python中的date7.time转换为date2

2024-04-16 09:14:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我收到了以下格式的日期时间

timestamp = '2019-03-18 01:50:00 -0800'

想在python中转换成datetime 我尝试过,但无法选择UTC偏移量

^{pr2}$

我得到的错误

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/_strptime.py", line 324, in _strptime
(bad_directive, format))
ValueError: 'z' is a bad directive in format '%Y-%m-%d %H:%M:%S %z'

如何在python2.7中选择UTC偏移量?在


Tags: informatdatetime格式错误line时间timestamp
2条回答

安装pytz库

sudo easy_install  upgrade pytz

将pytz导入代码

^{pr2}$

应该有帮助。在

只需使用dateutil

from dateutil import parser

obj = parser.parse('2019-03-18 01:50:00 -0800')
print(obj)
#2019-03-18 01:50:00-08:00

相关问题 更多 >