Since there's a chance that your excel files are coming from different computers/people; there's a chance that the formatting is messy; so be extra cautious.
I just imported data from 50 odd excels where the dates were entered in DD/MM/YYYY
or DD-MM-YYYY
, but most of the Excel files stored them as MM/DD/YYYY
(Probably because the PCs were setup with en-us
instead of en-gb
or en-in
).
Even more irritating was the fact that dates above 13/MM/YYYY
were in DD/MM/YYYY
format still. So there was variations within the Excel files.
The most reliable solution I figured out was to manually set the Date column on each excel file to to be Plain Text -- then use this code to parse it:
if date_str_from_excel:
try:
return datetime.strptime(date_str_from_excel, '%d/%m/%Y')
except ValueError:
print("Unable to parse date")