简体   繁体   English

Python:从字符串中解析ISO 8601日期和时间(使用标准模块)

[英]Python: Parse ISO 8601 date and time from a string (using the standard modules)

I want to parse the date for entries given by SVN: 我想解析SVN给出的条目的日期:

svn list --xml https://subversion:8765/svn/Foo/tags/

If I am not mistaken it is given using the ISO 8601 standard. 如果我没有弄错,则使用ISO 8601标准。 An example is: 一个例子是:

dateString = "2012-02-14T11:22:34.593750Z"

I am using Python 2.7 and am looking for the best way to process is using the standard modules only. 我正在使用Python 2.7,我正在寻找最好的处理方法,仅使用标准模块。 I think I´ll parse it this way using a regular expression: ^--- is a match group and A indicates that I will assume that they are always there (which might be a weakness) 我想我会用正则表达式解析它: ^---是一个匹配组, A表示我会认为它们总是存在(这可能是一个弱点)

dateString = "2012-02-14T11:22:34.593750Z"
              ^--- ^- ^-A^- ^- ^--------A
                        |               |
                        |  I think it always gives the date
                        |  as UTC, which Z means in ISO 8601.
                        |
                 Just a separator

After parsing it I´ll simply create a datetime from it. 解析之后,我只需从中创建一个日期时间。

Is there a better way using Python 2.7 with only the standard modules? 有没有更好的方法使用Python 2.7只有标准模块?

不需要正则表达式,而是使用datetime.datetime.strptime()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM