使用Python将CSV文件导入sqlite 3数据库表我有一个CSV文件,我想使用Python将这个文件批量导入sqlite 3数据库。命令是“.import.”。但它似乎不能像这样工作。有人能给我举一个在sqlite 3中怎么做的例子吗?我在用窗户以防万一。谢谢
3 回答
慕森卡
TA贡献1806条经验 获得超8个赞
import csv, sqlite3 con = sqlite3.connect(":memory:")cur = con.cursor()cur.execute("CREATE TABLE t (col1, col2);") # use your column names herewith open('data.csv','rb') as fin: # `with` statement available in 2.5+ # csv.DictReader uses first line in file for column headings by default dr = csv.DictReader(fin) # comma is default delimiter to_db = [(i['col1'], i['col2']) for i in dr]cur.executemany("INSERT INTO t (col1, col2) VALUES (?, ?);", to_db)con.commit()con.close()
添加回答
举报
0/150
提交
取消