当前正在执行一项已交给我的任务,该任务与机器学习部分相关。鉴于我可以为该函数提供2个参数,因此任务是将用户输入的字符串解析为日期: date_from 和 date_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进行开发和测试。
答案 0 :(得分:0)
可以帮助以下几个方面的模块:
calendar
模块(预安装)-> Link for more information datetime
模块(也已预装)-> Link for more information dateutil
模块(未预安装)。但是,使用pip
(pip install dateutil
)-> Link for more information 上面的模块中有大量的函数和方法,可让您使用日期(和时间)。
对于字符串的实际解析,您可能必须手动检查诸如“ to”和“ of”之类的词。但是,对于几个月(例如“七月”和“十二月”),您可以使用list
来存储它们,然后使用索引来计算月份之间的差异。