我的python csv解析器出了问题而且我不知道错误发生在哪里:
所以这是我的python:
# -*-coding:Utf-8 -*
import sqlite3;
from datetime import datetime, date;
conn = sqlite3.connect('info_max.sqlite3')
conn.text_factory = str
c = conn.cursor()
c.execute('drop table if exists info_max')
c.execute('create table info_max(id_huiles text, name_fr text, name_ln text, img text, mode_obt text, bien_vertus text, bio text)')
def mysplit(string):
quote = False
retval = []
current = ""
for char in string:
if char == '"':
quote = not quote
elif char == ',' and not quote:
retval.append(current)
current = ""
else:
current += char
retval.append(current)
return retval
# Lit de ligne en ligne, en sautant la première ligne
rawdata = open("info_max_try.csv", "r").read()
data = rawdata.split("\n", 1)[1:]
data = re.split('"END"', rawdata)
print(data)
for entry in data:
# Parse les valeurs
vals = mysplit(entry.strip())
# Convertit les strings format heure au format standard sqlite3
# Insert la ligne
vals[0] = int(vals[0])
print "Inserting %s..." %(vals[0])
print "The enrty is : %s" %entry
sql = "insert into info_max values(?, ?, ?, ?, ?, ?, ?)"
c.execute(sql, vals)
# Done !
conn.commit()
我的.csv
看起来像这样:
"id_huiles","name_fr", "name_ln", "img", "mode_obt", "bien_vertus","bio"
"77", "Basilic", "Basilium", "Basilic.png", "some_text", "some_text", "0"
...
我在3次参赛后得到了这个错误:
File "parseAromaHuile.py", line 39, in <module>
c.execute(sql, vals)
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 7, and there are 6 supplied.
修改
我的行中有\n
..我修改了我的代码,但我不想让我的解析器读取我的第一行
违规行看起来像这样:
"6","Bergamote", "Citrus bergamis L.", "bergamote.png", "some_text", "some_text\n other_text\n more_text","0"
感谢您的帮助!
答案 0 :(得分:1)
我认为问题出在你的mysplit
函数中,某处(如果/ elif似乎可疑),它会返回一个包含6个元素而不是7个元素的列表。
试试这个:
import sqlite3;
import csv
from datetime import datetime, date;
conn = sqlite3.connect('info_max.sqlite3')
conn.text_factory = str
c = conn.cursor()
c.execute('drop table if exists info_max')
c.execute('create table info_max(id_huiles text, name_fr text, name_ln text, img text, mode_obt text, bien_vertus text, bio text)')
with open('info_max.csv','rb') as source:
#I use csv reader instead of writing my own
data = csv.reader(source, delimiter=';')
header = data.next()
for vals in data:
for val in vals:
#I use replace function to get rid of qoutes
val.replace('"', '')
#adds the string "missing data" if some column is missing in your source
if len(vals) < 6:
vals += ['missing data' for i in range(0,6-len(vals))]
vals[0] = int(vals[0])
print "Inserting %s..." %(vals[0])
print "The enrty is : %s" %vals
sql = "insert into info_max values(?, ?, ?, ?, ?, ?, ?)"
c.execute(sql, vals)
conn.commit()
答案 1 :(得分:0)
您的特殊问题可能在于readline函数,该函数会将包含换行符的较长文本拆分为两个条目/行/数据集而不是一个! ; - )
解析csv的规范方法是使用Hrabal指出的csvreader-module。