Skip to content Skip to sidebar Skip to footer

Writing With Lxml Emitting No Whitespace Even When Pretty_print=true

I'm using the lxml library to read an xml template, insert/change some elements, and save the resulting xml. One of the elements which I'm creating on the fly using the etree.Eleme

Solution 1:

If you replace this line:

tree = etree.parse(r'template.xml')

with these lines:

parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse(r'template.xml', parser)

then it will work as expected. The trick is to use an XMLParser that has the remove_blank_text option set to True. Any existing ignorable whitespace will be removed and will therefore not disrupt the subsequent pretty-printing.

Post a Comment for "Writing With Lxml Emitting No Whitespace Even When Pretty_print=true"