pysqlite通过testring插入unicode数据8bit

2024-09-25 04:22:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道以前有人问过类似的问题,但这些答案似乎并不能说明我在这里做错了什么。在

我试图插入这一行: (Pdb)打印行 ['886','39','83474','0','0','0','0','0','1.00','D','20070813','R','C','B','SOCK 4PK','\xe9\x9e\x8b\xe5\xad\x90\xe5\xb0\xba\xe5\xaf\xb86-9.5/24-27.5CM','PR']

进入本表: 创建表项(“whs”int、“dept”int、“item”int、“dsun”int、“oh”int、“oh”rtv“int”、“adjp”int、“adjn”int、“sell”文本、“stat”文本、“lsldt”int、“cat1”文本、“cat2”文本、“cat3”文本、“des1”文本、“sgn3”文本、“unit”文本)

sgn3列似乎是问题的根源。它被定义为文本,要插入的数据是utf-8。为什么我收到sqlite3错误?在

ProgrammingError:'除非使用可以解释8位bytestr…=str的文本工厂,否则不能使用8位bytestrings。强烈建议您将应用程序切换到Unicode字符串

下面是执行插入的代码:

query = 'insert into %s values(%s)' % (
    self.tablename,
    ','.join(['?' for field in row])
)
self.con.execute(query, row)

下面是创建要插入的记录生成器的过程:

^{pr2}$

Tags: 答案文本selfquerypdbintrowsock
1条回答
网友
1楼 · 发布于 2024-09-25 04:22:02

这是我见过的最有用的错误消息之一。照它说的做。给它提供unicode对象,而不是UTF-8编码的str对象。换言之,丢失.encode('utf-8'),或者随后进行解码('utf-8')…什么才是csvdata?在

如果您在现有代码中遇到UnicodeDecodeError:

(1)你应该做一些比你想用它做的更有用的事情(把它扫到地毯下面)

(2)您可能希望将next更改为pass

回复评论

haha, it is a very useful error message

哈哈???我不是在开玩笑,它告诉你该怎么做。在

csvdata is a csv file in this case encoding using big5 in python 2.x

你叫什么“csv文件”:

(1) csvdata = open('my_big5_file', 'rb')
(2) csvdata = csv.reader(open('my_big5_file', 'rb'))
(3) other; please specify 

if I chose not to encode to utf-8, my rows are ascii right?

完全错了。bytes_read_from_file.decode('big5')生成一个unicode对象。你可能想读the Python Unicode HOWTO。在

so i need to explicitly change them to unicode before saving to the database?

不,它们已经unicode。但是,根据csvdata是什么,您可能需要将其编码到utf8中,以使它们通过csv机制,然后稍后对其进行解码。在

相关问题 更多 >