我有一个CSV文件,其中包含以下行:
10472;141507 some text.;810, smoe more text;city; 99,0060198416; -99,3360490152;some(a)textène, moretextèse;additional text s * (topl), again again text*;R : No retée;2015-02-03;
我想要实现的是插入由a分隔的值;进入一张桌子。
import csv
from dbconnect import connection
import codecs
cursor, conn = connection()
sql = """
INSERT INTO mddelcc
(field1, field2, field3, field4, field5, field6, field7, field8, field9, field10)
VALUES
(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);
"""
with codecs.open('file.csv', 'rb', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile, delimiter = ';', quoting=csv.QUOTE_NONE)
for row in reader:
cursor.execute(sql,row)
conn.commit()
cursor.close()
print("Done")
运行代码时,收到以下错误:
_mysql_exceptions.ProgrammingError:并非在字符串格式化期间转换所有参数
有人可以指出我正确的方向吗?
答案 0 :(得分:0)
您的字符串中有10个字段,但有11个值。
将sql更改为:
sql = """
INSERT INTO mddelcc
(field1, field2, field3, field4, field5, field6, field7, field8, field9, field10)
VALUES
(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);
"""
或者
sql = """
INSERT INTO mddelcc
(field1, field2, field3, field4, field5, field6, field7, field8, field9, field10,field11)
VALUES
(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);
"""