python - 从自然语言字符串中提取日期
问题描述
我正在使用 python 在字符串中查找日期,例如:
string01='los mantenimientos acontecieron en los dias 3,06,8 ,9, 15 y 29 de diciembre de 2018.Por cada mantenimiento fué cobrado $1,300.00 códigos de mantenimiento: (3)A34,(2)C54,(1)D65'
('维护会议是在 2018 年 12 月 3、06、8、9、15 和 29 日')
我首先尝试使用正则表达式来查找和拆分日期和(不是货币),然后将它们转换为预期的结果
预期结果:['3/12/2018','06/12/2018','08/12/2018','09/12/2018','15/12/2018','29/12/2018']
string02='los mantenimientos sucedieron en: 2,04,05,8,9,10,11,14,15,22,24, y 27 de junio de 2018.Valor de cada uno de los mantenimiento: $1,300.00, códigos de mantenimiento: (1)A35,(6)C54,(5)D65'
('2018 年 6 月 2、04、05、8、9、10、11、14、15、22、24 和 27 日发生的维护会议')预期结果:['02/06/2018','04/06/2018','05/06/2018','08/06/2018','09/06/2018','10/06/2018','11/06/2018','14/06/2018','15/06/2018','22/06/2018','24/06/2018','27/06/2018']
到目前为止,我已经尝试过:
dias=re.compile(r"((\s?[0-3]?[0-9]\s?\,?\s?){1,9}[0-3][0-9]|\sy\s[0-3][0-9]\sde\s(?:diciembre|junio)\sde\s[2][0][0-2][0-9])")
dias_found=re.findall(dias,string01)
但我得到元组和重复值:
[(' 3,06,8,9, 15', '9, '), (' y 29 de diciembre de 2018', '')]
应该是['3','06','8','9','15','29 de diciembre de 2018']
任何帮助将不胜感激。
提前致谢。
解决方案
您可以将re
模块与字符串操作一起使用来轻松提取日期
import requests
import re
import json
if __name__ == "__main__":
texts = [
'en los dias 3,06,8 ,9, 15 y 29 de diciembre de 2018.Por c',
'n en: 2,04,05,8,9,10,11,14,15,22,24, y 27 de junio de 2018.Valor de',
]
# select from the beginning of date-like text till the end of year
pattern = r'\s*((\d+[\sy\,]*)+[\D\s]+20\d{2})'
month_names = ['diciembre', 'junio'] # add others
month_pattern = re.compile(f'({"|".join(month_names)})', flags=re.IGNORECASE)
all_dates = []
for item in texts:
match = re.search(pattern, item)
if not match:
continue
date_region: str = match.group(1)
# find year
year = re.search('(20\d{2})', date_region).group(1)
# find month
month_match = re.search(month_pattern, date_region)
month = month_match.group(1)
# remove everything after month
date_region = date_region[: month_match.start()]
# find all numbers, we're assuming they represent day of the month
days = re.findall('(\d+)', date_region)
found_dates = [f'{d}/{month}/{year}' for d in days]
all_dates.append(found_dates)
print(all_dates)
我不知道葡萄牙语中的月份名称?(编辑:它是西班牙语),但用数字替换那些是一项微不足道的任务。输出:
[['3/diciembre/2018',
'06/diciembre/2018',
'8/diciembre/2018',
'9/diciembre/2018',
'15/diciembre/2018',
'29/diciembre/2018'],
['2/junio/2018',
'04/junio/2018',
'05/junio/2018',
'8/junio/2018',
'9/junio/2018',
'10/junio/2018',
'11/junio/2018',
'14/junio/2018',
'15/junio/2018',
'22/junio/2018',
'24/junio/2018',
'27/junio/2018']]
推荐阅读
- html - How do you remove the gap between inner divs & container div?
- vb.net - 用鼠标移动后,PictureBox 会在 Form1 上留下重影吗?
- discord - 在频道上获取第一条消息
- php - 复式会计报告
- javascript - Why can't I refer to the inherited method from JavaScript prototype within method
- asp.net-core - .net core modelstate 使用带有 guid 的选择列表无效
- arrays - 使用外部 JSON 文件而不是 .TS 文件进行预输入
- javascript - 数组不推送预期的元素
- python - Simple Client - Server scripts don't work, Timeout error 10060 when attempting to connect
- r - 稳健回归中的 MM 估计