Monday, June 27, 2011

minidom.writexml handling unicode errors

Problem:
Writing xml with unicode characters does not work, even with the encoding set to 'utf-8'. Always results in something like

UnicodeEncodeError('ascii', u'kr\xf6se1989', 2, 3, 'ordinal not in range(128)')


Solution (from here):
Use the codecs module and manually open the file in utf-8 mode for writing

import codecs
doc.writexml(codecs.open(fname,'wb','utf-8'), encoding='utf-8')

1 comment: