다양한 문자열에서 날짜를 식별하고 추출하려고 합니다. 날짜 형식이 동일하지 않을 수 있습니다. 나는 datefinder 패키지를 사용하고 있지만 출력을 저장하는 데 몇 가지 문제가 있습니다.
목표: 다양한 방식(예: 4월 22일, 4월 22일 또는 4월 22일 등)으로 형식이 지정될 수 있는 문자열에서 날짜를 추출하고 날짜가 없으면 값을 '없음'으로 설정하고 추가 날짜 또는 '없음'이 있는 날짜 목록.
아래의 예를 참조하십시오.
예 1: (날짜를 반환하지만 내 목록에 추가되지 않음)
import datefinder
extracted_dates = []
sample_text = 'As of February 27, 2019 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)
예 2: (날짜를 반환하지 않고 내 목록에 추가되지 않음)
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)
귀하의 패키지를 사용해 보았지만 귀하의 예제에서 실제 날짜를 추출하는 빠르고 일반적인 방법이 없는 것 같았습니다.
대신 DateParser 패키지와 더 구체적으로 search_dates 메소드를 사용했습니다.
나는 당신의 예에서만 간단히 테스트했습니다.
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)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다