我有几个这样的字符串:
"Programa Directrices de Gesti\xc3\xb3n Tur\xc3\xadstica"
我应该以这种方式存储在 xml 文件中
<content><![CDATA[Programa Directrices de Gestión Turística]]></content>
我使用这个代码:
from xml.dom import minidom
data_cdata = doc.createCDATASection(text)
cdv = doc.createElement(tag)
cdv.appendChild(data_cdata)
root.appendChild(cdv)
doc.appendChild(root)
但是输出是:
<content><![CDATA["Programa Directrices de Gesti\xc3\xb3n Tur\xc3\xadstica]]></content>
我该怎么做?
(对不起我的英语)
最佳答案
Python 并不像您希望的那样表示 ASCII 范围之外的字符。特殊字符 \xc3\xb3
和 \xc3\xad
与每个字符的十六进制序数相关:ó
和 í
.
看来您的代码没有很好地翻译特殊字符。它不是发布实际的 ó
和 í
而是发布它们各自的表示:\xc3\xb3
和 \xc3\xad
>。现在我对您使用的库一无所知,但我会在appendChild函数中搜索有关翻译的快速修复。如果找不到它,您也许可以使用循环遍历 text
删除特殊字符并将其转换为常规字母
(“ó”变为“o”)。
我希望我能提供更多帮助:)。
祝你好运
杰斯帕
关于python - 使用Python2.7处理xml文件中的特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54827373/