python - 使用 MySQLdb 对非 utf 符号进行 utf8 编码

标签 python mysql unicode encoding utf-8

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

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

我需要解析该值并将其存储在 mysql 数据库中。清理这些值的最佳方法是什么,以便我可以插入 "Valentie's Day" , "<copyright symbol> Ventures. All Rights Reserved." ?像这样的不同标记大约有 20 多种。

做顺子INSERT ,我会得到以下错误:

Warning: Incorrect string value: '\xA9 1987...' for column 'title' at row 1

最佳答案

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

title = text.encode('utf8')

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

关于python - 使用 MySQLdb 对非 utf 符号进行 utf8 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11182062/

相关文章:

javascript - 为什么 '❌' [0] === '❌' 但 '✔️' [0] !== '✔️' ?

python - 验证英语文本中 "a"和 "an"的正确使用 - Python

mysql - 如何统计同一张表中每个人的操作?

mysql - 使用外键插入MySQL,如果外键不存在,则自动创建新的外键

php - 使用 for 循环将数组实例插入表中

c++ - 使用多字节字符集的 MFC 应用程序中的 UTF-8 文本

Python CSV 文件 UTF-16 到 UTF-8 打印错误

Python - 从一系列 FreqDist 中获取最新出现的 FreqDist Key

python - 覆盖父类(super class)型的协程时如何使用 mypy?

c++ - 在没有 SWIG 的情况下使用 C++ 扩展 Python