【发布时间】:2014-12-28 17:12:19
【问题描述】:
感谢您的帮助。我是 SQLite3 的新手,并且确实将两者混为一谈。下面的代码我试过了,还是不行。
import csv
import sqlite3
# Create the database
connection = sqlite3.connect('addresses.db')
cursor = connection.cursor()
# Create the table
cursor.execute('DROP TABLE IF EXISTS addresses')
cursor.execute('''CREATE TABLE addresses
(ID text, Firstname text, Surname text, Address1 text, Address2 text, Postcode text)''')
connection.commit()
# Load the CSV file into CSV reader
csvfile = open('addresses.txt', 'r')
creader = csv.reader(csvfile, delimiter=',', quotechar='"')
# Iterate through the CSV reader, inserting values into the database
for t in creader:
cursor.execute('INSERT INTO addresses VALUES (?,?,?,?,?,?)', t )
# Close the csv file, commit changes, and close the connection
csvfile.close()
connection.commit()
connection.close()
我得到了错误
对于创建者中的 t:
解码中的文件“/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/encodings/ascii.py”,第 26 行
返回 codecs.ascii_decode(输入,self.errors)[0]
UnicodeDecodeError:“ascii”编解码器无法解码位置 0 的字节 0xe2:序数不在范围内(128)
【问题讨论】:
-
您说您想将一个名为
names.txt的文件加载到数据库中,但您确实加载了一个文件addresses.txt... 抱歉提出了明显的问题,但有时... :-) -
对不起,我的错字。感谢您的发现 - 我希望就这么简单!