【问题标题】:fuzzy timestamp parsing with Python使用 Python 进行模糊时间戳解析
【发布时间】:2009-08-11 06:29:11
【问题描述】:

是否有 Python 模块可以解释模糊时间戳,如 unix 中的 date 命令:

> date -d "2 minutes ago"
Tue Aug 11 16:24:05 EST 2009

到目前为止,我发现的最接近的是 dateutil.parser,但上面的示例失败了。

谢谢

【问题讨论】:

  • 您对示例中的“模糊”有何看法?您指定相对偏移量的事实?或者这样做的语法(“2分钟前”)? “N - 00:02:00”也会模糊吗?还是因为你包含了不断变化的“现在”的概念?

标签: python date parsing timestamp


【解决方案1】:

查看这个开源模块:parsedatetime

【讨论】:

    【解决方案2】:

    dateparser

    用法:

    >>> import dateparser
    >>> dateparser.parse('2 minutes ago')
    datetime.datetime(2018, 11, 27, 13, 44, 54, 993170)
    >>> dateparser.parse('yesterday at 15:12')
    datetime.datetime(2018, 11, 26, 15, 12)
    

    【讨论】:

      【解决方案3】:

      我一直在使用 pyparsing 来解决这个问题 - 你可以找到我最近的尝试 here。它适用于这些测试用例:

      today
      tomorrow
      yesterday
      in a couple of days
      a couple of days from now
      a couple of days from today
      in a day
      3 days ago
      3 days from now
      a day ago
      now
      10 minutes ago
      10 minutes from now
      in 10 minutes
      in a minute
      in a couple of minutes
      20 seconds ago
      in 30 seconds
      20 seconds before noon
      20 seconds before noon tomorrow
      noon
      midnight
      noon tomorrow
      

      【讨论】:

        猜你喜欢
        • 2020-07-14
        • 2021-07-25
        • 2018-07-02
        • 1970-01-01
        • 1970-01-01
        • 2017-04-13
        • 1970-01-01
        • 2021-06-30
        • 2016-11-11
        相关资源
        最近更新 更多