Parth
Parth

Reputation: 759

Escape unescaped data of XML string in python3

I want to escape the unescaped data inside a xml string e.g.

string = "<tag parameter = "something">I want to escape these >, < and &</tag>"

to

"<tag parameter = "something">I want to escape these &gt;, &lt; and &amp;</tag>"

Upvotes: 2

Views: 409

Answers (1)

icedtrees
icedtrees

Reputation: 6466

Perhaps you should be considering re.sub:

>>> oldString = '<tag parameter = "something">I want to escape these >, < and &</tag>'
>>> newString = re.sub(r"(<tag.*?>)(.*?)</tag>", lambda m: m.group(1) + cgi.escape(m.group(2)) + "</tag>", oldString)
>>> print newString
<tag parameter = "something">I want to escape these &gt;, &lt; and &amp;</tag>

My warning is that the regular expression will definitely break if you have nested tags. See Why is it such a bad idea to parse XML with regex?

Upvotes: 3

Related Questions