How to prevent xml.ElementTree fromstring from dropping commentnode
问题 I have tho following code fragment: from xml.etree.ElementTree import fromstring,tostring mathml = fromstring(input) for elem in mathml.getiterator(): elem.tag = 'm:' + elem.tag return tostring(mathml) When i input the following input : <math> <a> 1 2 3 </a> <b /> <foo>Uitleg</foo> <!-- <bar> --> </math> It results in: <m:math> <m:a> 1 2 3 </m:a> <m:b /> <m:foo>Uitleg</m:foo> </m:math> How come? And how can I preserve the comment? edit : I don't care for the exact xml library used, however, I