Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从字符串中识别和提取日期-Python_Python_Date_Nlp_Datefinder - Fatal编程技术网

从字符串中识别和提取日期-Python

从字符串中识别和提取日期-Python,python,date,nlp,datefinder,Python,Date,Nlp,Datefinder,我希望从许多不同的字符串中识别和提取日期。日期的格式可能不相同。我一直在使用datefinder软件包,但在保存输出时遇到了一些问题 目标:从字符串中提取日期,该字符串可以以多种不同的方式格式化(例如4月22日、4月22日或4月22日或4月22日等),如果没有日期,则将值设置为“无”,并在日期列表中附加日期或“无” 请看下面的例子 示例1:(返回日期,但不会附加到我的列表中) 示例2:(这不会返回日期,也不会附加到我的列表中) 我尝试过使用你的软件包,但似乎没有快速、通用的方法来提取你的示例中的

我希望从许多不同的字符串中识别和提取日期。日期的格式可能不相同。我一直在使用datefinder软件包,但在保存输出时遇到了一些问题

目标:从字符串中提取日期,该字符串可以以多种不同的方式格式化(例如4月22日、4月22日或4月22日或4月22日等),如果没有日期,则将值设置为“无”,并在日期列表中附加日期或“无”

请看下面的例子

示例1:(返回日期,但不会附加到我的列表中)

示例2:(这不会返回日期,也不会附加到我的列表中)


我尝试过使用你的软件包,但似乎没有快速、通用的方法来提取你的示例中的真实日期

相反,我使用了包,更具体地说是方法

我只是简单地用你的例子测试了一下

from dateparser.search import search_dates

sample_text = 'As of February 27, 2019 there were 28 dogs at the kennel.'
extracted_dates = []

# Returns a list of tuples of (substring containing the date, datetime.datetime object)
dates = search_dates(sample_text)

if dates is not None:
  for d in dates:
    extracted_dates.append(str(d[1]))
else:
  extracted_dates.append('None')

print(extracted_dates)

我在复制示例1时遇到了一些问题。运行脚本后,
extracted_dates
包含
['2019-02-27 00:00:00','2020-05-28 00:00:00']
@MikeXydas,其读数似乎是2020年5月28日的'28只狗'
import datefinder

extracted_dates = []
sample_text = 'As of the date, there were 28 dogs at the kennel.'

matches = datefinder.find_dates(sample_text)
for match in matches:
    if match == None:
        date = 'None'
        extracted_dates.append(date)
    else:
        date = str(match)
        extracted_dates.append(date)
from dateparser.search import search_dates

sample_text = 'As of February 27, 2019 there were 28 dogs at the kennel.'
extracted_dates = []

# Returns a list of tuples of (substring containing the date, datetime.datetime object)
dates = search_dates(sample_text)

if dates is not None:
  for d in dates:
    extracted_dates.append(str(d[1]))
else:
  extracted_dates.append('None')

print(extracted_dates)