私は得ています
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 0: ordinal not in range(128)
SQLAlchemyを使用してアクセスしているMySQLデータベースからのテキストをこの関数に渡す場合:
re.compile(ur"<([^>]+)>", flags=re.UNICODE).sub(u" ", s)
データベースのエンコーディングはutf-8であり、SQLAlchemyのcreate_engine関数にエンコーディングを渡します。
編集: これは私がデータベースにクエリを実行する方法です:
doc = session.query(Document).get(doc_id)
s = doc.title
提案により、s.decode('utf-8')をに渡しましたsub
。上記のエラーは消えましたが、別のドキュメントで別のエラーが発生します。
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;
どんな助けでも大歓迎です