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...)
io
module:fp=io.open('Documents\Write.xml', 'w', newline='\r\n') and then
write tofp
like thatet.write(fp, pretty_print=True)
(See docs.python.org/2/library/io.html#io.open) – Lelandet.write()
can take as input a filename or a open file pointer, like something coming fromio.open
(lxml.de/api/lxml.etree._ElementTree-class.html#write). You can tryimport io
thenet.write(io.open('Documents\Write.xml', 'w', newline='\r\n'), pretty_print=True)
– Lelandf=io.open('Documents\Write.xml', 'w', newline='\r\n')
andf.write(lxml.etree.tostring(et, pretty_print=True))
? – Leland