如何使用 Python strptime 解析例如 2010-04-24T07:47:00.007+02:00

发布于 2024-09-27 13:03:16 字数 398 浏览 1 评论 0原文

有谁知道如何使用Python的strptime方法解析标题中描述的格式?

我有类似的东西:

import datetime    
date = datetime.datetime.strptime(entry.published.text, '%Y-%m-%dT%H:%M:%S.Z')

我似乎无法弄清楚这是什么类型的时间格式。顺便说一下,我是Python语言的新手(我习惯了C#)。

更新

这是我根据下面的建议(答案)更改代码的方法:

from dateutil.parser import *
from datetime import *
date = parse(entry.published.text)

Does anyone know how to parse the format as described in the title using Pythons strptime method?

I have something similar to this:

import datetime    
date = datetime.datetime.strptime(entry.published.text, '%Y-%m-%dT%H:%M:%S.Z')

I can't seem to figure out what kind of timeformat this is. By the way, I'm a newbie at the Python language (I'm used to C#).

UPDATE

This is how I changed the code based on the advise (answers) below:

from dateutil.parser import *
from datetime import *
date = parse(entry.published.text)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无戏配角 2024-10-04 13:03:17

这是标准的 XML 日期时间格式 ISO 8601。如果您已经在使用 XML 库,那么大多数库都内置了日期时间解析器。xml.utils.iso8601 工作得相当好。

import xml.utils.iso8601
date = xml.utils.iso8601.parse(entry.published.text)

您可以在这里查看许多其他方法来处理这个问题:
http://wiki.python.org/moin/WorkingWithTime

That's the standard XML datetime format, ISO 8601. If you're already using an XML library, most of them have datetime parsers built in. xml.utils.iso8601 works reasonably well.

import xml.utils.iso8601
date = xml.utils.iso8601.parse(entry.published.text)

You can look at a bunch of other ways to deal with that here:
http://wiki.python.org/moin/WorkingWithTime

机场等船 2024-10-04 13:03:16

该日期采用 ISO 8601,或更具体地说 RFC 3339 格式。

此类日期无法使用 strptime 进行解析。有一个 Python 问题 讨论了这个问题。

dateutil.parser.parse 可以处理各种日期,包括您示例中的那个。

如果您使用外部模块进行 XML 或 RSS 解析,则其中可能有一个例程来解析该日期。

That date is in ISO 8601, or more specifically RFC 3339, format.

Such dates can't be parsed with strptime. There's a Python issue that discusses this.

dateutil.parser.parse can handle a wide variety of dates, including the one in your example.

If you're using an external module for XML or RSS parsing, there is probably a routine in there to parse that date.

谜兔 2024-10-04 13:03:16

这是找到答案的好方法:使用 strftime 构造一个格式字符串,该字符串将发出您所看到的内容。根据定义,该字符串将是使用 strptime 解析时间所需的字符串。

Here's a good way to find the answer: using strftime, construct a format string that will emit what you see. That string will, by definition, be the string needed to PARSE the time with strptime.

各空 2024-10-04 13:03:16

如果您尝试解析 RSS 或 Atom 提要,请使用通用提要解析器。它支持许多日期/时间格式

>>> import feedparser                 # parse feed
>>> d = feedparser.parse("http://stackoverflow.com/feeds/question/3946689")
>>> t = d.entries[0].published_parsed # get date of the first entry as a time tuple
>>> import datetime
>>> datetime.datetime(*t[:6])         # convert time tuple to datetime object
datetime.datetime(2010, 10, 15, 22, 46, 56)

If you are trying to parse RSS or Atom feeds then use Universal Feed Parser. It supports many date/time formats.

>>> import feedparser                 # parse feed
>>> d = feedparser.parse("http://stackoverflow.com/feeds/question/3946689")
>>> t = d.entries[0].published_parsed # get date of the first entry as a time tuple
>>> import datetime
>>> datetime.datetime(*t[:6])         # convert time tuple to datetime object
datetime.datetime(2010, 10, 15, 22, 46, 56)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文