加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

是否有任何python库可以从自然语言中解析日期和时间?

发布时间:2020-12-20 10:51:19 所属栏目:Python 来源:网络整理
导读:? 我正在寻找的是可以将“明天早上6点”或“中午的下一个模拟”转换为适当的日期时间对象。 ? 解决方案 parsedatetime - 能够解析“人类可读”日期/时间表达式的Python模块。 #!/usr/bin/env python from datetime import datetime import parsedatetime as

?

我正在寻找的是可以将“明天早上6点”或“中午的下一个模拟”转换为适当的日期时间对象。

?

解决方案


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

本文首发于Python黑洞网,博客园同步更新

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读