本文介绍了在Python中解析时间字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期时间字符串,我不知道如何在Python中解析它。

I have a date time string that I don't know how to parse it in Python.

字符串是这样的:

Tue May 08 15:14:45 +0800 2012

我试过

datetime.strptime(Tue May 08 15:14:45 +0800 2012,% a%b%d%H:%M:%S%z%Y)

但Python引发

'z'是格式为'%a%b%d%H:%M:%S%z%Y'的错误指令

根据Python doc:

According to Python doc:

%z  UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive).

我想知道解析此时间字符串的正确格式是什么?

I wonder what is the right format to parse this time string?

推荐答案

datetime.datetime.strptime 在时区解析方面有问题。看看:

datetime.datetime.strptime has problems with timezone parsing. Have a look at the dateutil package:

>>> from dateutil import parser
>>> parser.parse("Tue May 08 15:14:45 +0800 2012")
datetime.datetime(2012, 5, 8, 15, 14, 45, tzinfo=tzoffset(None, 28800))

这篇关于在Python中解析时间字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 05:20