本文介绍了在Python中解析人类格式的日期范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些类似于字符串的人类日期范围,如下所示:

I have some human-style date ranges, in strings, like the following:

22-24th April 2012
14-23 July
20th June - 5th July

我想用Python解析这些对象,以便最终得到两个datetime对象:一个用于开始,一个用于结束.

I want to parse these in Python so that I can end up with two datetime objects: one for the start, one for the end.

有没有可以让我做到这一点的模块?我已经尝试过parsedatetime,并且看起来其中的evalRange函数可以做到这一点(请参见 http://code-bear.com/code/parsedatetime/docs/index.html 以获得文档),但它似乎根本没有解析任何内容,只是返回当前日期/次,两次.

Is there any module that will let me do this? I've tried parsedatetime, and it looks like the evalRange function within that may do it (see http://code-bear.com/code/parsedatetime/docs/index.html for documentation), but it doesn't seem to parse anything at all, and just returns the current date/time, twice.

有什么想法吗?

推荐答案

我最终编写了一个Python模块来执行此操作,现在我已将其开源.可以从 Github 下载,其中有,并且可以使用以下方法从PyPI安装:

I ended up writing a Python module to do this, which I have now open-sourced. It is available for download on Github, there is documentation, and it can be installed from PyPI using:

pip install daterangeparser

对于感兴趣的人,该模块通过使用 PyParsing 创建完整的解析器来工作,这是一个很棒的(而且非常容易实现,使用)工具.

For those who are interested, the module works by creating a full parser using PyParsing, a great (and remarkably easy-to-use) tool.

这篇关于在Python中解析人类格式的日期范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 04:57