Python 在非utf符号上使用MySQLdb进行Utf8编码

Python 在非utf符号上使用MySQLdb进行Utf8编码,python,mysql,unicode,encoding,utf-8,Python,Mysql,Unicode,Encoding,Utf 8,我收到一个xml提要,其值如下: <Theme>Valentine&#39;s Day</Theme> <Copyright>&#169; Ventures. All Rights Reserved.</Copyright> 指定编码,然后将字符串编码为utf8 # -*- coding: utf-8 -*- title = text.encode('utf8') 如果您使用真正的XML解析器解析XML,您将得到文本形式的Uni

我收到一个xml提要,其值如下:

<Theme>Valentine&#39;s Day</Theme>
<Copyright>&#169; Ventures. All Rights Reserved.</Copyright>

指定编码,然后将字符串编码为
utf8

# -*- coding: utf-8 -*-
title = text.encode('utf8')

如果您使用真正的XML解析器解析XML,您将得到文本形式的Unicode字符串。然后可以使用UTF-8对其进行编码:

title = text.encode('utf8')

标题可以写入数据库,尽管许多细节仍不清楚,因为我们不知道您是如何写入数据库的。

文件编码在这里是不相关的,它只影响字符串文本的解释方式。
title = text.encode('utf8')