python - 如何在Python中正确从文本中提取各种日期格式

标签 python date text-extraction datefinder

我必须从 PDF 中提取所有可用日期,然后检查契约(Contract)日期。

首先,我想提取我从 PDF 中提取的文本中的所有日期。现在日期可以采用多种格式。我尝试在下面的示例中添加所有口味的日期。

我尝试使用Datefinder Python 模块来提取所有日期。尽管它很接近,但最初抛出了一些垃圾日期,而且也与第一个日期不正确匹配

import datefinder

dateContent = """ Test
I want to apply for leaves August,​ ​11,​ ​2017 I want to apply for leaves Aug, 23, 2017 I want to apply for leaves Aug, 21, 17 
I want to apply for leaves August 20 2017
I want to apply for leaves August 30th, 2017 I want to apply for leaves August 31st 17
I want to apply for leaves 8/26/2017 I want to apply for leaves 8/27/17
I want to apply for leaves 28/8/2017 I want to apply for leaves 29/8/17 I want to apply for leaves 30/08/17
I want to apply for leaves 15 Jan 17 I want to apply for leaves 14 January 17
I want to apply for leaves 13 Jan 2017
I want to apply for leaves Jan 10 17 I want to apply for leaves Jan 11 2017 I want to apply for leaves January 12 2017
"""

matches = datefinder.find_dates(dateContent)

for match in matches:
    print(match)

回应:

2019-08-05 00:00:00

2019-06-11 00:00:00

2017-06-05 00:00:00

2017-08-23 00:00:00

2017-08-21 00:00:00

2017-08-20 00:00:00

2017-08-30 00:00:00

2017-08-31 00:00:00

2017-08-26 00:00:00

2017-08-27 00:00:00

2017-08-28 00:00:00

2017-08-29 00:00:00

2017-08-30 00:00:00

2017-01-15 00:00:00

2017-01-14 00:00:00

2017-01-13 00:00:00

2017-01-10 00:00:00

2017-01-11 00:00:00

2017-01-12 00:00:00

如您所见,我有 17 个这样的 Date 对象,但我得到了 19 个。从底部检查,最后 16 个匹配正确。然后就是那些最初的垃圾。 一旦我正确地获得了这些日期,我就可以继续使用某种 N-Gram 模型来检查哪个日期上下文是契约(Contract)信息。

任何解决问题的帮助都会很棒。

最佳答案

我解决了这个问题。 实际上我的文本内容存在一些编码问题。

dateContent = dateContent.replace(u'\u200b', '')

用空字符替换\u200b 解决了该问题。 Datefinder 模块完成查找所有不同日期格式的其余工作。

关于python - 如何在Python中正确从文本中提取各种日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56459712/

相关文章:

python - 使用特定规则重新排序 pandas DataFrame

python - 如何从列表中检索具有相同外键的元素?

python - 敌人不停留在原地

java - 如何在android中将毫秒转换为日期格式?

javascript - 开 Jest ,匹配正则表达式

ios - 使用MuPDF从PDF提取文本时,文本的最后一行被截断

python - 如何在 Python 中加速 json 解码和编码操作?

使用 MySQL 显示表状态的 php 日期格式

java - 从文本文件中提取数据以制作公历

python - 有没有办法使用可读性和 python 来提取文本,而不是 HTML?