Datetime string reformatting

Mark Lawrence breamoreboy at
Sun Dec 22 12:32:39 CET 2013

On 22/12/2013 10:58, Igor Korot wrote:
> Hi, ALL,
> I am trying to comprehend what is required to get a successful processing. ;-)
> My script receives a data from the csv file. In this csv file there is
> a datetime field.
> This datetime field is formatted as follows: %m/%d/%Y
> %H:%M:%S.{milliseconds}. I'm reading this field into the string with
> this format.
> The trouble comes from the fact that I need to insert this value into
> mySQL. Doing it directly gives me an error: "Incorrect formatting".
> After a bit of googling and trial I found out that mySQL is looking
> for the format of '%Y/%m/%d %H:%M:%S.{milliseconds}.
> There is a mySQL function which transfers the data into the proper
> format: STR_TO_DATE(), but I can't obviously call it since it is not
> known to Python.
> Now obviously there should be a possibility to reformat this string.
> I guess I can try to do:
> mydate = date(mydate_from_csv)
> text = mydate.format()
> but this feels like an ugly hack.
> Is there a better alternative?
> Thank you.

Round trip the data using these or 
use string slicing and dicing.

My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

More information about the Python-list mailing list