title: Unescape _xHHHH_ XML escape sequences using Python
body: <p>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 <code>_xHHHH_</code> convention e.g. ASCII BEL aka U+0007 is represented by the 7-character sequence <code>u"_x0007_"</code>. 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].</p>
  
  <p>Here is my best attempt at unescapeing the parser output as efficiently as possible:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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.</p>
  
  <p>The question: Any better ideas out there?</p>