简体   繁体   中英

python convert string time to sql datetime format

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)

Please check this. Comments inline with code.

from datetime import datetime

v = ['4/29/2016 8:25:58 AM', '5/25/2016 2:22:22 PM', 'True', 'Foo', 1]

def fixdate(_params):

        print "Before changing format ..."
        print _params
        #First date in list
        tstamp = datetime.strptime(_params[0], "%m/%d/%Y %I:%M:%S %p")
        #Add %p after %S if AM or PM is required
        newtstamp = datetime.strftime(tstamp, "%m-%d-%Y %I:%M:%S")
        #Update the element in list
        _params[0] = newtstamp

        #Second date in list
        tstamp = datetime.strptime(_params[1], "%m/%d/%Y %I:%M:%S %p")
        newtstamp = datetime.strftime(tstamp, "%m-%d-%Y %I:%M:%S")
        #Update the element in list
        _params[1] = newtstamp

        print "After changing format..."
        print _params

fixdate(v)

Output:

C:\Users\dinesh_pundkar\Desktop>python c.py
Before changing format ...
['4/29/2016 8:25:58 AM', '5/25/2016 2:22:22 PM', 'True', 'Foo', 1]
After changing format...
['04-29-2016 08:25:58', '05-25-2016 02:22:22', 'True', 'Foo', 1]

C:\Users\dinesh_pundkar\Desktop>

Code with list comprehension:

from datetime import datetime

v = ['4/29/2016 8:25:58 AM', '5/25/2016 2:22:22 PM', 'True', 'Foo', 1]

def fixdate(_params):

        print "Before changing format ..."
        print _params

        _params = [i if ':' not in str(i) and '/' not in str(i) else datetime.strftime(datetime.strptime(i, "%m/%d/%Y %I:%M:%S %p"), "%m-%d-%Y %I:%M:%S") for i in _params]

        print "After changing format..."
        print _params

fixdate(v)

Output:

C:\Users\dinesh_pundkar\Desktop>python c.py
Before changing format ...
['4/29/2016 8:25:58 AM', '5/25/2016 2:22:22 PM', 'True', 'Foo', 1]
After changing format...
['04-29-2016 08:25:58', '05-25-2016 02:22:22', 'True', 'Foo', 1]

C:\Users\dinesh_pundkar\Desktop>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM