我正在尝试将我的csv文件导入到我的表中。我按照here找到了类似的步骤,但是我收到了错误:
test.csv
"a",1,1
"b",2,2
"c",3,3
错误:
Traceback (most recent call last):
File "C:/Users/Sam/Desktop/big data/workspace/yelpdatabase.py", line 19, in <module>
to_db = [(i['letter'], i['num1'], i['num2']) for i in dr]
File "C:/Users/Sam/Desktop/big data/workspace/yelpdatabase.py", line 19, in <listcomp>
to_db = [(i['letter'], i['num1'], i['num2']) for i in dr]
KeyError: 'letter'
代码:
import csv
import sqlite3
#Create The Database
connection = sqlite3.connect('test.db')
#create cursor
cursor = connection.cursor()
#Create table
cursor.execute('DROP TABLE IF EXISTS testtable')
cursor.execute('CREATE TABLE testtable ( letter text, num1 int, num2, int)')
#Load the CSV file into CSV reader
path = r"C:\Users\Sam\Desktop\big data\workspace\test.csv"
with open(path,'r') 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['letter'], i['num1'], i['num2']) for i in dr]
cursor.executemany("INSERT INTO t (letter, num1, num2) VALUES (?, ?, ?);", to_db)
#commit changes, and close the connection
connection.commit()
connection.close()
编辑:我上传了test.csv文件
答案 0 :(得分:2)
如果不查看CSV,很难确定是什么问题,但“KeyError”表示DictReader生成的字典没有名为“letter”的键。
仔细检查CSV的第一行是否有“字母”。例如,如果我的CSV看起来像这样:
头像1,HEAD2,头像3
1,2,3
然后提取数据的正确行将是:
to_db = [(i["head1"], i["head2"], i["head3"]) for i in dr]
注意 - 键区分大小写,因此在上面的示例中,“head1”有效,但“Head1”会生成KeyError。
编辑:感谢您发布CSV。问题似乎是您没有标题行,因此DictReader获取字段名["a","1","1"]
(您可以通过调用dr.fieldnames
来检查)。
要使代码无需修改CSV,您可以将DictReader行更改为:
csv.DictReader(fin, ["letter", "num1", "num2"])
这将手动将标题“letter”,“num1”和“num2”分配给您的三列。然后你可以这样做:
to_db = [(i["letter"], i["num1"], i["num2"]) for i in dr]