我有2个.txt文件,我使用https://convertio.co/csv-xlsx/将它们转换为.csv文件。现在,我想使用Python中的SQLite将这两个.csv文件导入两个数据库(UI是Jupyter Notebook)。这两个.csv文件标记为 person.csv 和 person_votes.csv 。所以,我按照这里给出的代码(Importing a CSV file into a sqlite3 database table using Python)来做到了:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<textarea id="sample"></textarea>
我不明白为什么当我尝试执行上面的代码时,我不断收到错误消息:“KeyError:'personid'”。有人可以帮忙吗?
另外,如果我在同一个Python文件中为文件 person_votes.csv 创建另一个名为 to_db2 的数据库表,那么下面的查询会给我所有两个表之间的常见元素:
import sqlite3, csv
con = sqlite3.connect(":memory:")
cur = con.cursor()
cur.execute("CREATE TABLE person (personid STR,age STR,sex STR,primary_voting_address_id STR,state_code STR,state_fips STR,county_name STR,county_fips STR,city STR,zipcode STR, zip4 STR, PRIMARY KEY(personid))")
with open('person.csv','r') as person_table: # `with` statement available in 2.5+
# csv.DictReader uses first line in file for column headings by default
dr = csv.DictReader(person_table) # comma is default delimiter
#personid age sex primary_voting_address_id state_code state_fips county_name county_fips city zipcode zip4
to_db = [(i['personid'], i['age'], i['sex'], i['primary_voting_address_id'], i['state_code'], i['state_flips'], i['county_name'], i['county_fips'], i['city'], i['zipcode'], i['zip4']) for i in dr]
cur.executemany("INSERT INTO t (age, sex) VALUES (?, ?);", to_db)
con.commit()
上面两个.csv文件的链接在这里:https://drive.google.com/open?id=0B-cyvC6eCsyCQThUeEtGcWdBbXc。
答案 0 :(得分:1)
这适用于Windows 10,但也适用于Linux / Unix。有几个问题:
此外,我在文件而不是内存中创建数据库。它足够小,性能不应该成为问题,并且您所做的任何更改都将被保存。
这是我更正的文件(你可以自己做另一张桌子):
import sqlite3, csv
# con = sqlite3.connect(":memory:")
con = sqlite3.connect("person.db")
cur = con.cursor()
cur.execute("CREATE TABLE person (personid STR,age STR,sex STR,primary_voting_address_id STR,state_code STR,state_fips STR,county_name STR,county_fips STR,city STR,zipcode STR, zip4 STR, PRIMARY KEY(personid))")
with open('person.csv','r') as person_table:
dr = csv.DictReader(person_table, delimiter='\t') # comma is default delimiter
to_db = [(i['personid'], i['age'], i['sex'], i['primary_voting_address_id'], i['state_code'], i['state_fips'], i['county_name'], i['county_fips'], i['city'], i['zipcode'], i['zip4']) for i in dr]
cur.executemany("INSERT INTO person VALUES (?,?,?,?,?,?,?,?,?,?,?);", to_db)
con.commit()
答案 1 :(得分:0)
您的printf
声明中可能会遗漏一些列名。
将主键保留为INSERT INTO ...
也可能不太好。