My program basically read an input file, makes an lxml.etree from that file, than for example I add a node to the etree and then I want to print it back on a file. So to write it back on a file I use:
et.write('Documents\Write.xml', pretty_print=True)
And the output I have is:
<Variable Name="one" RefID="two"><Component Type="three"><Value>four</Value></Component></Variable>
While I'd like something like:
<Variable Name="one" RefID="two">
<Component Type="three">
<Value>four</Value>
</Component>
</Variable>
Where am I mistaken? I've tried many solutions but none seems to work (beautifulsoup, tidy, parser...)
Don't use the standard parser.
Use a custom parser with remove_blank_text=True
.
parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse(self.output_file, parser=parser)
# Do stuff with the tree here
tree.write(your_output_file, pretty_print=True)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With