我收到一个 xml feed,其值如下:
<Theme>Valentine's Day</Theme>
<Copyright>© 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/