简体   繁体   中英

special characters in xml file with Python2.7

I have several strings like this:

"Programa Directrices de Gesti\\xc3\\xb3n Tur\\xc3\\xadstica"

That I should store in an xml file in this way

<content><![CDATA[Programa Directrices de Gestión Turística]]></content>

I use this code:

from xml.dom import minidom

data_cdata = doc.createCDATASection(text)
cdv = doc.createElement(tag)
cdv.appendChild(data_cdata)
root.appendChild(cdv)
doc.appendChild(root)

but the output is:

<content><![CDATA["Programa Directrices de Gesti\xc3\xb3n Tur\xc3\xadstica]]></content>

How i can do it?

(sorry for my english)

Python does not represent characters outside of the ascii range like you would like. The special characters \\xc3\\xb3 and \\xc3\\xad are related to hexadecimal ordinals of each character: ó and í . It seems like your code does not translate the special characters very well. Instead of posting the actual ó and í it posts their respective representations: \\xc3\\xb3 and \\xc3\\xad . Now I know nothing of the library you use, but I would search in the appendChild function for a quick fix regarding the translation. If you cannot find it, you could perhaps iterate over the text with a loop removing special characters and turning them into regular letters ("ó" into "o").

I wish I could be of more help :).

Good luck,

Jesper

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM