问题描述
我正在获得
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 0: ordinal not in range(128)
当我传递来自MySQL数据库的文本,我正在使用SQLAlchemy访问,这个功能:
when I pass text coming from a MySQL database, which I am accessing using SQLAlchemy, to this function:
re.compile(ur"<([^>]+)>", flags=re.UNICODE).sub(u" ", s)
数据库编码为utf-8,我甚至将编码传递给SQLAlchemy的create_engine函数。
The database encoding is utf-8 and I am even passing the encoding to the create_engine function of SQLAlchemy.
编辑:
这是我查询数据库的方式:
This is how I am querying the database:
doc = session.query(Document).get(doc_id)
s = doc.title
通过建议,我将s.decode('utf-8')传递给 sub
。上面的错误消失了,但是我得到一个不同的错误:
By suggestion, I passed s.decode('utf-8') to sub
. The error above disappeared, but I get a different error for a different document:
UnicodeDecodeError: 'utf8' codec can't decode byte 0xeb in position 449: invalid continuation byte
数据库表的定义如下:
CREATE TABLE `articles` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) DEFAULT NULL,
`cdate` datetime DEFAULT NULL,
`link` varchar(255) DEFAULT NULL,
`content` text,
UNIQUE KEY `id` (`id`),
UNIQUE KEY `link_idx` (`link`)
) ENGINE=InnoDB AUTO_INCREMENT=4127834 DEFAULT CHARSET=utf8;
任何帮助将不胜感激
推荐答案
我已经解决了这个问题。 标题
列由SQLAlchemy作为 str
而不是 Unicode
。
我以为添加 encoding ='utf8'
作为 create_engine
的参数会照顾这个,但是,正确的方法是在数据库中传递URI: mysql:// my @ myserver / mydatabase?charset = utf8
。
I have solved the issue. The title
column was being returned by SQLAlchemy as a str
and not Unicode
.I thought adding encoding='utf8'
as an argument to create_engine
would take care of this, however, the right way to do it is to pass it in the database URI: mysql://me@myserver/mydatabase?charset=utf8
.
感谢您的所有答案!
这篇关于SQLAlchemy和UnicodeDecodeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!