我知道以前有人问过类似的问题,但这些答案似乎并不能说明我在这里做错了什么。在
我试图插入这一行: (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}$
这是我见过的最有用的错误消息之一。照它说的做。给它提供
unicode
对象,而不是UTF-8编码的str
对象。换言之,丢失.encode('utf-8')
,或者随后进行解码('utf-8')…什么才是csvdata
?在如果您在现有代码中遇到UnicodeDecodeError:
(1)你应该做一些比你想用它做的更有用的事情(把它扫到地毯下面)
(2)您可能希望将
next
更改为pass
回复评论
哈哈???我不是在开玩笑,它告诉你该怎么做。在
你叫什么“csv文件”:
完全错了。
bytes_read_from_file.decode('big5')
生成一个unicode
对象。你可能想读the Python Unicode HOWTO。在不,它们已经
unicode
。但是,根据csvdata
是什么,您可能需要将其编码到utf8
中,以使它们通过csv机制,然后稍后对其进行解码。在相关问题 更多 >
编程相关推荐