【问题标题】:Parse xsd:dateTime formatted string to python datetime将 xsd:dateTime 格式化字符串解析为 python 日期时间
【发布时间】:2012-09-11 11:37:38
【问题描述】:

给定xsd:dateTime format 中的字符串,我想创建一个python 日期时间对象。 例如,我特别需要能够解析像这样的字符串 '2012-09-23T09:55:00',但所有其他定义的示例也应该正确解析,并使用时区。

【问题讨论】:

标签: python datetime xsd timezone


【解决方案1】:

使用datetime.datetime.strptime class method 解析这些:

dt = datetime.datetime.strptime(xsdDateTime, '%Y-%m-%dT%H:%M:%S')

您的示例确实包含时区。如果您确实需要时区支持,最好使用python-dateutil module

from dateutil.parser import parse
dt = parse(xsdDateTime)

【讨论】:

  • 没有名为解析编辑的模块:该模块被称为解析器而不是解析,但它现在似乎可以工作
  • @MartinFlucka:已更正,我的错误。
【解决方案2】:

我也会推荐 python-dateutil 包。 我使用的是 2.7.3 版,它也有一个 isoparser。 但是,isoparser 不仅仅解析时间字符串,而且我已经填写了一个问题。 请改用解析器模块。

from dateutil.parser import parse
dt = parse(xsdDateTime)

hth, 亚历克斯

【讨论】:

    猜你喜欢
    • 2019-09-14
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多