from __future__ import unicode_literals, print_function idioteque = "\x1b]2;I hate cpython\x07" def test_lxml(): from lxml import etree root = etree.Element('root') root.text = idioteque s = etree.tostring(root) print('s = ', s) tree = etree.fromstring(s) print(tree) def test_elementtree(): from xml.etree.ElementTree import tostring, fromstring, Element root = Element('root') root.text = idioteque s = tostring(root) print('s = ', s) tree = fromstring(s) if __name__== '__main__': test_elementtree() #test_lxml()