Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the default indentation of etree.tostring in lxml

I have an XML document which I'm pretty-printing using lxml.etree.tostring

print etree.tostring(doc, pretty_print=True)

The default level of indentation is 2 spaces, and I'd like to change this to 4 spaces. There isn't any argument for this in the tostring function; is there a way to do this easily with lxml?

like image 907
Eli Courtwright Avatar asked Aug 06 '09 13:08

Eli Courtwright


People also ask

What is LXML Etree in Python?

The lxml XML toolkit is a Pythonic binding for the C libraries libxml2 and libxslt. It is unique in that it combines the speed and XML feature completeness of these libraries with the simplicity of a native Python API, mostly compatible but superior to the well-known ElementTree API.


2 Answers

Since version 4.5, you can set indent size using indent() function.

etree.indent(root, space="    ")
print(etree.tostring(root))
like image 56
kuch Avatar answered Sep 20 '22 02:09

kuch


As said in this thread, there is no real way to change the indent of the lxml.etree.tostring pretty-print.

But, you can:

  • add a XSLT transform to change the indent
  • add whitespace to the tree, with something like in the cElementTree library

code:

def indent(elem, level=0):
    i = "\n" + level*"  "
    if len(elem):
        if not elem.text or not elem.text.strip():
            elem.text = i + "  "
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
        for elem in elem:
            indent(elem, level+1)
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
    else:
        if level and (not elem.tail or not elem.tail.strip()):
            elem.tail = i
like image 28
ThibThib Avatar answered Sep 20 '22 02:09

ThibThib