跳过CSV fi的第一行

2024-03-29 06:58:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我的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如下所示:

^{pr2}$

我在输入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"

谢谢你的帮助!在


Tags: csvtextininfoexecutedatacurrentconn
0条回答
网友
1楼 · 发布于 2024-03-29 06:58:46

我认为问题出在你的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()
网友
2楼 · 发布于 2024-03-29 06:58:46

您的特殊问题可能在于readline函数,它将包含新行的较长文本拆分为两个条目/行/数据集,而不是一个!;—)

解析csv的标准方法是使用Hrabal指出的csvreader模块。在

相关问题 更多 >