I have a large CSV with hundreds of columns that I want to ingest into SQL server. I'm using Pandas to read it as a dataframe and invoking a stored procedure through pyodbc to write the data. I've done this before many times with smaller CSVs. In this case, I have several columns that represent datetime values. The problem is that in a single column I can have rows that use different formats for the date:
'2020-07-08T09:00:01+00:00'
'2020-07-08T05:00:07Z'
'2020-07-08T06:40:23.000Z'
I can parse these all with this
from dateutil import parser
z1 = parser.isoparse(z)
where z is any of those strings. Here's my dilemma. If I just write these as string, the call to sql likes the format '2020-07-08T06:40:23.000Z', but not the others. So I want to write in that format.
So I try
date_format = "%Y-%m-%dT%H:%M:%S.%fZ"
parser.isoparse(z).strftime(frm)
BUT, that format writes the string '2012-11-29T16:33:00.000000Z', not '2012-11-29T16:33:00.000Z' There must be exactly 3 zeros between the last period and the Z. I tried using "%3f" but strftime() does not like that. I could just "fix it" with my own function to pull out the extra "0"s, but that seems tacky. It seems like there should be some way to get it write directly like I want (the way sql seems to expect). What is the incantation?