Encoding issues with python's etree.tostring Encoding issues with python's etree.tostring xml xml

Encoding issues with python's etree.tostring


You're encoding the text twice. Try this:

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


etree.tostring(elem, encoding=str)

will return str but not binary in Python 3

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