如何解析 ISO 8601 格式的日期? [英] How do I parse an ISO 8601-formatted date?

查看:46
本文介绍了如何解析 ISO 8601 格式的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析 RFC 3339 字符串,如 "2008-09-03T20:56:35.450686Z" 到 Python 的 datetime 类型.

我找到了 strptime 在 Python 标准库中,但不是很方便.

最好的方法是什么?

解决方案

isoparse 函数来自 python-dateutil

python-dateutil 包具有 dateutil.parser.isoparse 进行解析不仅是问题中的 RFC 3339 日期时间字符串,还有其他 ISO 8601 日期和时间不符合 RFC 3339 的字符串(例如没有 UTC 偏移量的字符串,或仅表示日期的字符串).

<预><代码>>>>导入 dateutil.parser>>>dateutil.parser.isoparse('2008-09-03T20:56:35.450686Z') # RFC 3339 格式datetime.datetime(2008, 9, 3, 20, 56, 35, 450686, tzinfo=tzutc())>>>dateutil.parser.isoparse('2008-09-03T20:56:35.450686') # ISO 8601 扩展格式datetime.datetime(2008, 9, 3, 20, 56, 35, 450686)>>>dateutil.parser.isoparse('20080903T205635.450686') # ISO 8601 基本格式datetime.datetime(2008, 9, 3, 20, 56, 35, 450686)>>>dateutil.parser.isoparse('20080903') # ISO 8601 基本格式,仅限日期datetime.datetime(2008, 9, 3, 0, 0)

python-dateutil 包也有 dateutil.parser.parse.和 isoparse 相比,估计没那么严格,但是两者都比较宽容,会尝试解释你传入的字符串.如果你想消除任何误读的可能性,你需要使用比这两个函数都更严格的东西.

与 Python 3.7+ 的内置对比 datetime.datetime.fromisoformat

datutil.parser.isoparse 是一个完整的 ISO-8601 格式解析器,但 fromisoformat 故意不是.请参阅后一个函数的文档以了解此警告.(请参阅此答案).

I need to parse RFC 3339 strings like "2008-09-03T20:56:35.450686Z" into Python's datetime type.

I have found strptime in the Python standard library, but it is not very convenient.

What is the best way to do this?

解决方案

isoparse function from python-dateutil

The python-dateutil package has dateutil.parser.isoparse to parse not only RFC 3339 datetime strings like the one in the question, but also other ISO 8601 date and time strings that don't comply with RFC 3339 (such as ones with no UTC offset, or ones that represent only a date).

>>> import dateutil.parser
>>> dateutil.parser.isoparse('2008-09-03T20:56:35.450686Z') # RFC 3339 format
datetime.datetime(2008, 9, 3, 20, 56, 35, 450686, tzinfo=tzutc())
>>> dateutil.parser.isoparse('2008-09-03T20:56:35.450686') # ISO 8601 extended format
datetime.datetime(2008, 9, 3, 20, 56, 35, 450686)
>>> dateutil.parser.isoparse('20080903T205635.450686') # ISO 8601 basic format
datetime.datetime(2008, 9, 3, 20, 56, 35, 450686)
>>> dateutil.parser.isoparse('20080903') # ISO 8601 basic format, date only
datetime.datetime(2008, 9, 3, 0, 0)

The python-dateutil package also has dateutil.parser.parse. Compared with isoparse, it is presumably less strict, but both of them are quite forgiving and will attempt to interpret the string that you pass in. If you want to eliminate the possibility of any misreads, you need to use something stricter than either of these functions.

Comparison with Python 3.7+’s built-in datetime.datetime.fromisoformat

datutil.parser.isoparse is a full ISO-8601 format parser, but fromisoformat is deliberately not. Please see the latter function's docs for this cautionary caveat. (See this answer).

这篇关于如何解析 ISO 8601 格式的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆