我试图从CSV文件插入MySQL,文件中的第一个'列'是日期这种格式:ValueError:时间数据'{0}'与格式'%d /%m /%Y'不匹配
31/08/2017;
然后我在表列设置为YYYY-MM-DD
这是我的代码:
import datetime
import csv
import MySQLdb
...
insertionSQL="INSERT INTO transactions (trans_date, trans_desc, trans_amnt) VALUES(" + datetime.datetime.strptime('{0}', '%d/%m/%Y').strftime('%Y-%m-%d') + ",{2},{3}), row)"
cur.execute(insertionSQL)
我从我的Python脚本收到以下错误:
(data_string, format))
ValueError: time data '{0}' does not match format '%d/%m/%Y'
好的。首先,这似乎与MySQL无关,因为'datetime.datetime.strptime('{0}','%d /%m /%Y')给出了这个警告。 – sam
您应格式化字符串,即“{0}”格式(your_date_string)。 – temasso