0

An issue occurs when I try to find a date in a .txt file using datefinder. I have the feeling I am unnecessarily switching between data types to obtain the result I desire.

Underneath is a MWE which results in generator object, which in turn is empty when changed to a list. I would like to obtain a datetime in the format %d-%m-%Y. MWE:

import datefinder 

f = ['this is text', 'this is a date', '* Model creation date:            Sun Apr 25 08:52:06 2021']
for line in f:
    if "creation date" in line: 
        date_line = str(line)
        rev_date = datefinder.find_dates(_date_line)
Frits
  • 113
  • 1
  • 8

2 Answers2

2

dateutil's parser seems to do a better job:

import dateutil

f = ['this is text', 'this is a date', '* Model creation date:            Sun Apr 25 08:52:06 2021']
dates = []

for line in f:
    try:
        dates.append(dateutil.parser.parse(line, fuzzy=True))
    except dateutil.parser.ParserError:
        pass
    
print(dates)    
# [datetime.datetime(2021, 4, 25, 8, 52, 6)]

For the specific use-case:

for line in f:
    if "* Model creation date:" in line:
        rev_date = dateutil.parser.parse(line, fuzzy=True)
        break
print(rev_date)    
# 2021-04-25 08:52:06
FObersteiner
  • 22,500
  • 8
  • 42
  • 72
  • This option works for the the presented MWE. However I would like to find the specific line '* Model creation date: Sun Apr 25 08:52:06 2021' in the list from a much larger dataset than presented, which also contains more dates that are irrelevant when advancing in the script. – Frits Jun 16 '21 at 14:53
  • @Frits sure, made an edit to include that specific case. – FObersteiner Jun 16 '21 at 15:00
  • @Frits glad I could help! You might also want to catch the case if the specific line containing the date is *not* found. – FObersteiner Jun 16 '21 at 15:15
2

Seems datefinder.find_dates works based on :. If you can remove : character after creation date get right result. If always your string include creation date: you can remove this substring after if statement:

import datefinder 

f = ['this is text', 'this is a date', '* Model creation date:            Sun Apr 25 08:52:06 2021']
for line in f:
    if "creation date" in line: 
        date_line = line.replace('creattion date:', '')
        rev_date = datefinder.find_dates(date_line)
Ali Irani
  • 557
  • 3
  • 10