python unicode escaping to xmlescape

If you get errors like..

UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in position 48: ordinal not in range(128)

try and escape them to something supported locally..
(from http://www.amk.ca/python/howto/unicode)

particularly


u.encode('ascii', 'xmlcharrefreplace')


>>> u = unichr(40960) + u'abcd' + unichr(1972)
>>> u.encode('utf-8')
'\xea\x80\x80abcd\xde\xb4'
>>> u.encode('ascii')
Traceback (most recent call last):
File "", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode character '\ua000' in position 0: ordinal not in range(128)
>>> u.encode('ascii', 'ignore')
'abcd'
>>> u.encode('ascii', 'replace')
'?abcd?'
>>> u.encode('ascii', 'xmlcharrefreplace')
'ꀀabcd޴'