79

我有一个日期时间字符串,我不知道如何在 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' is a bad directive in format '%a %b %d %H:%M:%S %z %Y'

根据 Python 文档:

%z UTC 偏移量,格式为 +HHMM 或 -HHMM(如果对象是幼稚的,则为空字符串)。

解析这个时间字符串的正确格式是什么?

4

5 回答 5

114

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))
于 2012-05-08T07:28:36.833 回答
31

Your best bet is to have a look at strptime()

Something along the lines of

>>> from datetime import datetime
>>> date_str = 'Tue May 08 15:14:45 +0800 2012'
>>> date = datetime.strptime(date_str, '%a %B %d %H:%M:%S +0800 %Y')
>>> date
datetime.datetime(2012, 5, 8, 15, 14, 45)

Im not sure how to do the +0800 timezone unfortunately, maybe someone else can help out with that.

The formatting strings can be found at http://docs.python.org/library/time.html#time.strftime and are the same for formatting the string for printing.

Hope that helps

Mark

PS, Your best bet for timezones in installing pytz from pypi. ( http://pytz.sourceforge.net/ ) in fact I think pytz has a great datetime parsing method if i remember correctly. The standard lib is a little thin on the ground with timezone functionality.

于 2012-05-08T07:29:05.443 回答
7

这是一个 stdlib 解决方案,它支持输入时间字符串中的可变 utc 偏移量:

>>> from email.utils import parsedate_tz, mktime_tz
>>> from datetime import datetime, timedelta
>>> timestamp = mktime_tz(parsedate_tz('Tue May 08 15:14:45 +0800 2012'))
>>> utc_time = datetime(1970, 1, 1) + timedelta(seconds=timestamp)
>>> utc_time
datetime.datetime(2012, 5, 8, 7, 14, 45)
于 2015-08-09T15:03:19.990 回答
3

它已经在 SO 中讨论过很多次。简而言之,不支持“%z”,因为平台不支持。我的解决方案是一个新的,只是跳过时区。:

    datetime.datetime.strptime(re.sub(r"[+-]([0-9])+", "", "Tue May 08 15:14:45 +0800 2012"),"%a %b %d %H:%M:%S %Y")
于 2012-05-08T07:50:17.027 回答
1
In [117]: datetime.datetime.strptime?
Type:           builtin_function_or_method
Base Class:     <type 'builtin_function_or_method'>
String Form:    <built-in method strptime of type object at 0x9a2520>
Namespace:      Interactive
Docstring:
    string, format -> new datetime parsed from a string (like time.strptime()).
于 2012-05-08T07:24:31.483 回答