python convert string time to sql datetime format
<p>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 <code>4/22/2016 1:00:00 PM</code>. Each date is part of a larger list that might have non-date type items in it. for example:</p>

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

<p>I would like to reformat every date (if present in the list) with the correct MySQL format of </p>

<p><code>%m-%d-%Y %I:%M:%S</code></p>

<p>How would i do this with a <strong>list comprehension</strong>? 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 <code>v</code>. </p>

<pre><code>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)
</code></pre>

<p>Please check this. <strong>Comments inline with code.</strong></p>

<pre><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)
</code></pre>

<p><strong>Output:</strong></p>

<pre><code>C:\Users\dinesh_pundkar\Desktop&gt;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&gt;
</code></pre>

<p><strong>Code with list comprehension:</strong></p>

<pre><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

        _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)
</code></pre>

<p><strong>Output:</strong></p>

<pre><code>C:\Users\dinesh_pundkar\Desktop&gt;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&gt;
</code></pre>

python mysql datetime