<分区> 分区>
我刚开始使用 Python,并且正在使用以下代码将 CSV 文件导入到 sqlite3 表中,我承认我已经从互联网上复制了大部分内容:
with open(getPathTo('my.csv'), 'r') as csvfile:
reader = csv.DictReader(csvfile)
records = [(row['SEGMENT'], row['Comp 1'], row['Comp 2']) for row in reader]
c.executemany("INSERT INTO comparison (`SEGMENT`, `Comp 1`, `Comp 2`) VALUES (?,?,?);", records)
conn.commit()
它工作正常,但我对很多文件和表格重复这个,我想把它变成一个函数。我的目标是:
def importCSVToTable(file, table, columns)
但是,给定一个列列表,我该如何在这一行中使用它:
records = [(row['SEGMENT'], row['Comp 1'], row['Comp 2']) for row in reader]
我想我只是有点被语法迷住了。
