convert escaped unicode web entities to unicode

def unescape_utf8(msg):
    ''' convert escaped unicode web entities to unicode '''
    def sub(m):
        text = m.group(0)
        if text[:3] == "&#x": return unichr(int(text[3:-1], 16))
        else:                 return unichr(int(text[2:-1]))
    return re.sub("&#?\w+;", sub, urllib.unquote(msg))