【问题标题】:Python - Date parser that could get all the dates from a specific string inputPython - 可以从特定字符串输入中获取所有日期的日期解析器
【发布时间】:2018-08-09 07:17:54
【问题描述】:

目前正在执行一项与机器学习部分相关的任务。任务是将用户输入的字符串解析为日期,因为我可以为函数提供 2 个参数:date_fromdate_to; > 类似于下面给出的示例。

任何可以提供帮助的类/功能或有关如何做的想法都会对我有很大帮助。

2.a 8th of July -> 2018-07-08
2.b July to December -> 2018-07 to 2018-12
2.c July -> 2018-07
2.d July 1st -> 2018-07-01
2.e Last week -> 2018-08-02 (curr_date - 7days)
2.f next week -> 2018-08-16 (curr_date + 7days)
2.g today -> 2018-08-09
2.h tomorrow -> 2018-08-10
2.i next monday -> 2018-08-13
2.j last monday -> 2018-07-30
2.k next month -> 2018-09 (or curr_date + 30days)
2.l last month -> 2018-07 (or curr_date - 30days)

目前我正在使用 Flask 进行开发和测试。

【问题讨论】:

    标签: python datetime flask date-parsing string-to-datetime


    【解决方案1】:

    几个可以提供帮助的模块:

    1. calendar 模块(预装)--> Link for more information
    2. datetime 模块(也预装)--> Link for more information
    3. dateutil 模块(未预装)。但是,使用pip (pip install dateutil) --> Link for more information 安装它很容易

    上面的模块中有大量的函数和方法可以让你处理日期(和时间)。

    至于字符串的实际解析,您可能必须手动检查“to”和“of”之类的词。但是,对于几个月(如“七月”和“十二月”),您可以使用 list 来存储它们,然后使用索引计算月份的差异。

    【讨论】:

    • 嗨,您能否提供一个示例代码,说明我将如何解析这些输入。例如 8 月 10 日至 08-10-2018。我只需要一个想法让我开始。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多