2

As soon as I modify the tail of an element (default is None), writing with pretty_print deletes all indentation. Everything is on a single line.

Combining pretty_print and tail is not possible ?

Example:

from lxml import etree as et

root = et.Element("MY_DOC")

date = et.SubElement(root, "date")
date.text = "2014-09-29"
date.tail="\n"  # to separate more the following section

name = et.SubElement(root, "name")
name.text = "should be 2 lines after"

tree=et.ElementTree(root)
tree.write('output.xml', encoding="UTF-8", pretty_print=True)
Eric H.
  • 2,152
  • 4
  • 22
  • 34
  • Could you explain why do you need to manually put a newline into a tail? (if you pretty print - it would be put there any way) Thanks. – alecxe Jan 24 '15 at 19:04
  • Just to beautify more for reading under editor: one "\n" more (which would make 2, if it worked). – Eric H. Jan 28 '15 at 15:22
  • To insert a comment, see https://stackoverflow.com/a/36321035/703421 pretty_print will respect indentation. – Eric H. Oct 27 '20 at 14:41

0 Answers0