[ACCEPTED]-Encoding issues with python's etree.tostring-tostring

Accepted answer
Score: 20

You're encoding the text twice. Try this:

import xml.etree.cElementTree as etree
elem = etree.Element('tag')
elem.text = u"Würth Elektronik Midcom"
xml = etree.tostring(elem, encoding='UTF-8')


Score: 4

etree.tostring(elem, encoding=str)

will return str but not binary in Python 3

You can 6 also serialise to a Unicode string without 5 declaration by passing the unicode function 4 as encoding (or str in Py3), or the 3 name 'unicode'. This changes the return 2 value from a byte string to an 1 unencoded unicode string.

More Related questions