简体   繁体   English

无法使用Python3在MySQL中插入CSV

[英]Unable to insert CSV in MySQL using Python3

I have a CSV file that has the following lines: 我有一个包含以下行的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;

What I am trying to achieve is to insert the values delimited by a ; 我要达到的目的是插入由a分隔的值; into a table. 到一张桌子。

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")

When running the code, I receive the following error: 运行代码时,出现以下错误:

_mysql_exceptions.ProgrammingError: not all arguments converted during string formatting _mysql_exceptions.ProgrammingError:在字符串格式化期间并非所有参数都已转换

Can someone please point me in the right direction ? 有人能指出我正确的方向吗?

You have 10 fields in your string but 11 values. 您的字符串中有10个字段,但有11个值。

Change sql to this: 将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);
"""

or to this 或对此

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);
"""

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM