Unescape _xHHHH_ XML escape sequences using Python

Posted by John Machin on Stack Overflow See other posts from Stack Overflow or by John Machin
Published on 2009-06-23T03:39:47Z Indexed on 2010/05/22 17:30 UTC
Read the original article Hit count: 325

Filed under:
|
|

I'm using Python 2.x [not negotiable] to read XML documents [created by others] that allow the content of many elements to contain characters that are not valid XML characters by escaping them using the _xHHHH_ convention e.g. ASCII BEL aka U+0007 is represented by the 7-character sequence u"_x0007_". Neither the functionality that allows representation of any old character in the document nor the manner of escaping is negotiable. I'm parsing the documents using cElementTree or lxml [semi-negotiable].

Here is my best attempt at unescapeing the parser output as efficiently as possible:

import re
def unescape(s,
    subber=re.compile(r'_x[0-9A-Fa-f]{4,4}_').sub,
    repl=lambda mobj: unichr(int(mobj.group(0)[2:6], 16)),
    ):
    if "_" in s:
         return subber(repl, s)
    return s

The above is biassed by observing a very low frequency of "_" in typical text and a better-than-doubling of speed by avoiding the regex apparatus where possible.

The question: Any better ideas out there?

© Stack Overflow or respective owner

Related posts about python

Related posts about Xml