Reputation: 1191
I have to read through a ton of files based on their file name. They can vary from the format Y-M-D, YMD, M_D_Y, or Y_M_D. There could be others but at the moment this is all I am given to work with.
I need to be able to extract the dates, which I have already done using a regular expression, and format them into the form YMD. For example if my input string is 06_12_2018, I need to be able to format that into 20180612 so I can do comparisons with another file later.
What I have tried so far:
def cleanDate(date):
datePatterns = [“%Y%m%d”, “%Y_%m_%d”, “%Y-%m-%d”, “%m_%d_%Y”]
for pattern in datePatterns:
if date in datePatterns:
return datetime.strftime(date, “%Y%m%d”)
else:
print “String format not found!”
return
Now that I am looking at it, it does not make sense to do if date in datePatterns
. What is the best way to approach this?
Upvotes: 3
Views: 128
Reputation: 362786
The best way will be to use try/except:
for pattern in datePatterns:
try:
return datetime.strptime(date, pattern)
except ValueError:
pass
else:
# none of the datePatterns worked
raise Exception('failed to parse')
Note that it is strptime
you want here, not strftime
. Reminder for the wetware: p is for parsing, f is for formatting.
They can vary from the format Y-M-D, YMD, M_D_Y, or Y_M_D. There could be others but at the moment this is all I am given to work with.
If there could be other formats, consider to use dateutil.parser
instead, which uses heuristics to guess the format. It's fairly popular, battle-tested and reliable.
>>> from dateutil.parser import parse # pip install python-dateutil
>>> parse("2018-05-12")
datetime.datetime(2018, 5, 12, 0, 0)
Upvotes: 3