I have a list of strings and I want to drop items that are not a date. I simply want to check if they are a date, not convert them to datetime. I am using dateutil.parser
- how can I handle the error it raises and continue the loop? Here is my code:
from dateutil.parser import parse
def drop_nondates(dates, fuzzy=False):
for string in dates:
try:
parse(string, fuzzy=fuzzy)
except:
dates.remove(string)
pass
return dates
Testing on small list:
In: drop_nondates( ['January 2018', 'Enero 2018', 'not a date 2018', '2018'] )
>>> ['January 2018', 'not a date 2018', '2018']
but desired output would be:
['January 2018', '2018']