I am importing a large csv file to ETL into a database, and the date format that was originally set in the csv file looks like 4/22/2016 1:00:00 PM. Each date is part of a larger list that might have non-date type items in it. for example:
v = ['4/29/2016 8:25:58 AM', '5/25/2016 2:22:22 PM', 'True', 'Foo', 1]
I would like to reformat every date (if present in the list) with the correct MySQL format of
%m-%d-%Y %I:%M:%S
How would i do this with a list comprehension? my code is not working for obvious reasons but i'm not sure where to go from here. I need to retain the index that the date is found in v.
from datetime import datetime, date, time
v = ['4/29/2016 8:25:58 AM', '5/25/2016 2:22:22 PM', 'True', 'Foo', 1]
def fixdate(_params):
tstamp = datetime.strptime(_params, "%m/%d/%Y %I:%M:%S %p")
newtstamp = date.strftime(tstamp, "%m-%d-%Y %I:%M:%S")
replace = { _params: newtstamp }
l = [replace.get(x, x) for x in _params]
print l
fixdate(v)