1

I have a CSV file which i am loading into MySQL DB, one of the fields is DataTime in NTP time format ex: 10:14:18.531 gmt Wed May 15 2013 I want to convert it to an acceptable format to store it in MySQL like 2013-05-15 10:14:18.

Should i do this to the CSV file before start loading it to MySQL or it can be done while loading the CSV file (to locate the NTP column on CSV file and convert it while loading)

I have been told that this can be done using Python please help.

Thanks in advance.

2

1 Answer 1

0

You can convert your NTP-formatted dates into datetime using strptime function while loading the CSV file:

from datetime import datetime
import MySQLdb


ntp_string = "10:14:18.531 gmt Wed May 15 2013"
ntp_datetime = datetime.strptime(ntp_string, "%H:%M:%S.%f %Z %a %b %d %Y")

db = MySQLdb.connect(host='...', user='...', passwd='...', db='...')
cursor = db.cursor()

cursor.execute('INSERT INTO test (id, mydate) VALUES (%s, %s)', (1, ntp_datetime))

Hope that helps.

1
  • Hi @alecxe, Thank you very mush for your useful information. I am loading a csv file into MySQL and i was successful using Python using the below script: inline import MySQLdb c = db.cursor() c.execute("load data local infile '/Users/Desktop/dtest.csv' into table tt lines terminated by '\n'") , My question is how can i use datetime.strptime while loading the CSV file, lets say the NTP field is the 2nd in CSV file. Cheers Jul 19, 2013 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.