是否有用于从自然语言解析日期和时间的python库?

发布于 2021-01-29 19:35:48

我正在寻找的东西可以将“明天凌晨6点”或“下一天中午”转换为适当的日期时间对象。

关注者
0
被浏览
88
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    parsedatetime -
    Python模块能够解析“人类可读”日期/时间表达式。

    #!/usr/bin/env python
    from datetime import datetime
    import parsedatetime as pdt # $ pip install parsedatetime
    
    cal = pdt.Calendar()
    now = datetime.now()
    print("now: %s" % now)
    for time_string in ["tomorrow at 6am", "next moday at noon", 
                        "2 min ago", "3 weeks ago", "1 month ago"]:
       print("%s:\t%s" % (time_string, cal.parseDT(time_string, now)[0]))
    

    输出量

    now: 2015-10-18 13:55:29.732131
    tomorrow at 6am:    2015-10-19 06:00:00
    next moday at noon: 2015-10-18 12:00:00
    2 min ago:  2015-10-18 13:53:29
    3 weeks ago:    2015-09-27 13:55:29
    1 month ago:    2015-09-18 13:55:29
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看