动态csv到sqlite导入

时间:2013-06-07 15:44:14

标签: python sqlite list dynamic csv

with open((path + 'stuff.csv'),'r+') as fin1:
    dr1 = csv.DictReader(fin1)
    create1="CREATE TABLE LGD ("
    exe1="INSERT INTO LGD ("
    for a in dr1.fieldnames:
        if a=='':
            pass
        elif ' ' in a:
            create1+=a.replace(" ","")+','
            exe1+=a.replace(" ","")+','
        else:
            create1 += a + ","
            exe1+= a + ","
    create1 = create1[:-1]
    exe1=exe1[:-1]
    create1 +=");"
        exe1+=") VALUES ("
    for a in range(len(dr1.fieldnames)-1):
        exe1+='?'+","
    exe1=exe1[:-1]
    exe1+=");"
    cur.execute(create1)
    to_db1 = [(a[dr1.fieldnames[0]],a[dr1.fieldnames[1]],a[dr1.fieldnames[2]],a[dr1.fieldnames[3]],a[dr1.fieldnames[4]]) for a in dr1]
    cur.executemany(exe1,to_db1)
    conn.commit()

好的我的目标是将每个csv文档作为表导入sqlite数据库。假设我有100个。

现在我的代码动态写入字符串,但是如何创建to_db1列表,所以我不必定义有多少列?

特别是这一行

to_db1 = [(a[dr1.fieldnames[0]],a[dr1.fieldnames[1]],a[dr1.fieldnames[2]],a[dr1.fieldnames[3]],a[dr1.fieldnames[4]]) for a in dr1]

我已经看到了这个问题,但我的不同之处在于,因为字段名中有空格而且它们不能被删除,因为许多文档都是不可编辑的。我也不想丢失下面显示的列表格式。

[(stuff,stuff,stuff),(stuff,stuff,stuff)]

Dynamically import columns from csv into database table in sqlite3 and python

重述我的问题,我想创建to_db1列表而不在编译时定义列名。它应该以这种格式创建

[(stuff,stuff,stuff),(stuff,stuff,stuff)] 

在运行时。

1 个答案:

答案 0 :(得分:1)

要获得没有空格且已经用逗号连接的字段名,您可以使用列表解析:

names = ['my _id', 'some a', 'some b']
sanitized_names = "({})".format(", ".join(x.replace(' ','') for x in names))

# => (my_id, somea, someb)

从那里,您可以重复使用您链接的答案。

要使用dr1订购dr1.fieldnames中的字母来获取元组,请使用[tuple(a[x] for x in dr1.fieldnames) for a in dr1]