def _xmlcharref_encode(unicode_data, encoding):
 chars = []
 for char in unicode_data:
  try:
   chars.append(char.encode(encoding, u'strict'))
  except UnicodeError:
   chars.append((u'&#%i;' % ord(char)))
 return u''.join(chars)

def _xmlcharref_encode ( unicode_data , encoding ) : chars = [ ] for char in unicode_data : try : chars . append ( char . encode ( encoding , u'strict' ) ) except UnicodeError : chars . append ( ( u'&#%i;' % ord ( char ) ) ) return u'' . join ( chars )
emulate python 2 .