将unicode插入sqlite? [英] Inserting unicode into sqlite?
问题描述
我仍在学习 Python,作为一个小项目,我编写了一个脚本,该脚本将获取我在文本文件中的值并将它们插入到 sqlite3 数据库中.但是有些名称有奇怪的字母(我猜你会称它们为非 ASCII),并且在出现时会产生错误.这是我的小脚本(请告诉我是否有可能更 Pythonic):导入 sqlite3
I am still learning Python and as a little Project I wrote a script that would take the values I have in a text file and insert them into a sqlite3 database. But some of the names have weird letter (I guess you would call them non-ASCII), and generate an error when they come up. Here is my little script (and please tell me if there is anyway it could be more Pythonic): import sqlite3
f = open('complete', 'r')
fList = f.readlines()
conn = sqlite3.connect('tpb')
cur = conn.cursor()
for i in fList:
exploaded = i.split('|')
eList = (
(exploaded[1], exploaded[5])
)
cur.execute('INSERT INTO magnets VALUES(?, ?)', eList)
conn.commit()
cur.close()
它会产生这个错误:
Traceback (most recent call last):
File "C:\Users\Admin\Desktop\sortinghat.py", line 13, in <module>
cur.execute('INSERT INTO magnets VALUES(?, ?)', eList)
sqlite3.ProgrammingError: You must not use 8-bit bytestrings unless you use a te
xt_factory that can interpret 8-bit bytestrings (like text_factory = str). It is
highly recommended that you instead just switch your application to Unicode str
ings.
推荐答案
要将文件内容转换为 unicode,您需要从它所在的任何编码进行解码.
看起来您使用的是 Windows,因此最好选择 cp1252
.
如果您从其他地方获得该文件,则所有赌注都将取消.
To get the file contents into unicode you need to decode from whichever encoding it is in.
It looks like you're on Windows so a good bet is cp1252
.
If you got the file from somewhere else all bets are off.
编码排序后,一种简单的解码方法是使用 codecs
模块,例如:
Once you have the encoding sorted, an easy way to decode is to use the codecs
module, e.g.:
import codecs
# ...
with codecs.open('complete', encoding='cp1252') as fin: # or utf-8 or whatever
for line in fin:
to_insert = (line.split('|')[1], line.split('|')[5])
cur.execute('INSERT INTO magnets VALUES (?,?)', to_insert)
conn.commit()
# ...
这篇关于将unicode插入sqlite?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!