Datetime string reformatting

Igor Korot ikorot01 at gmail.com
Sun Dec 22 05:58:10 EST 2013


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.



More information about the Python-list mailing list