Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从CSV到pysqlite的固定列数_Python_Csv_Excel 2003_Python 2.6_Pysqlite - Fatal编程技术网

Python 从CSV到pysqlite的固定列数

Python 从CSV到pysqlite的固定列数,python,csv,excel-2003,python-2.6,pysqlite,Python,Csv,Excel 2003,Python 2.6,Pysqlite,我在Windows8上使用的是Python 2.6(32位)。其目的是从逗号分隔的CSV文件中构建pysqlite数据库,其中第一行是列名。 我的代码如下(跳过了一些代码): def BuildDatabaseFromCSV(自身,文件名): 以文件(文件名“rb”)作为源: csvreader=csv.reader(源) first_entry=csvreader.next() self.BuildTable(第一个\u条目) #自我补充(csvreader) 对于csvreader中的条目:

我在Windows8上使用的是Python 2.6(32位)。其目的是从逗号分隔的CSV文件中构建pysqlite数据库,其中第一行是列名。

我的代码如下(跳过了一些代码):

def BuildDatabaseFromCSV(自身,文件名):
以文件(文件名“rb”)作为源:
csvreader=csv.reader(源)
first_entry=csvreader.next()
self.BuildTable(第一个\u条目)
#自我补充(csvreader)
对于csvreader中的条目:
自我补遗(条目)
def构建表(自身、cols_名称):
cmd_line=str(cols_name)[1:-1]#从列表类型中删除“[]”
cmd_line=“创建表%s(%s)”%(自身名称,cmd_行)
self._cursor.execute(命令行)#创建表
self._db.commit()
self.\u col\u num=len(cols\u名称)
def附加项(自身、条目):
长度=长度(条目)
如果self._col_num>length:#填充空字段
条目+=(['']*(自身列数-长度))
elif self._col_num<长度:#裁剪额外字段
条目=条目[:self.\u col\u num]
cmd_line=“插入%s值(%s)”\
%(self.\t_name,(“?,”*self.\u col_num)[:-1])
self.\u cursor.execute(命令行,条目)
self._db.commit()
def附录(自身、条目):
cmd_line=“插入%s值(%s)”\
%(self.\t_name,(“?,”*self.\u col_num)[:-1])
self.\u cursor.executemany(命令行,条目)
self._db.commit()
最初,我使用AddEntries()将CSV文件中的条目添加到数据库中。但是,对于Excel 2003生成的CSV文件,每行的“字段”数量可能不同。如果某些行包含尾随的空列或一些额外的垃圾,则它们可能比“列名”行包含更多或更少的字段(多余或缺少逗号)。

我不能使用“字段大小限制”,因为我需要先读取CSV来定义它。

有没有更简单的方法来达到以下目的?
1.读取CSV文件时,指定固定数量的列,以便自动插入或删除逗号?
2.向mysqlite数据库插入条目时,请指定固定数量的列,以便它可以接受可变数量的输入列?

提前谢谢

def BuildDatabaseFromCSV(self, file_name):
    with file(file_name, 'rb') as source:   
        csvreader = csv.reader(source)
        first_entry = csvreader.next()
        self.BuildTable(first_entry)
        #self.AddEntries(csvreader)
        for entry in csvreader:
            self.AddEntry(entry)

def BuildTable(self, cols_names):
    cmd_line = str(cols_names)[1:-1]   #Remove '[]' from list type    
    cmd_line = "create table %s (%s)" % (self._t_name, cmd_line)
    self._cursor.execute(cmd_line)  #Creating table
    self._db.commit()
    self._col_num = len(cols_names)

def AddEntry(self, entry):
    length = len(entry)
    if self._col_num > length: #Padding empty fields
        entry += ([''] * (self._col_num - length))
    elif self._col_num < length: #Crop extra fields
        entry = entry[:self._col_num]
    cmd_line = "insert into %s values (%s)"\
        % (self._t_name, ("?," * self._col_num)[:-1])
    self._cursor.execute(cmd_line, entry)
    self._db.commit()

def AddEntries(self, entries):
    cmd_line = "insert into %s values (%s)"\
        % (self._t_name, ("?," * self._col_num)[:-1])
    self._cursor.executemany(cmd_line, entries)
    self._db.commit()