Pretty_print in etree.tostring () xml python

I am trying to print an xml document with the pretty_print parameter. But this is a mistake

TypeError: tostring () received an unexpected keyword argument 'pretty_print'

Did I miss something?

def CreateXML2():
    Date = etree.Element("Date", value=time.strftime(time_format, time.localtime()));
    UserNode = etree.SubElement(Date, "User");
    IDNode = etree.SubElement(UserNode, "ID");
    print(etree.tostring(Date, pretty_print=True));
+5
source share
2 answers

It seems that the problem is that the library ElementTreedoes not support pretty printed. The workaround, as explained here , is to redisplay the output string from ElementTreeto another library that provides print support. A.

+2
source

Have you viewed this post on StackOverflow? I think it covers what you want:

in-place font format

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

effbot.org

, tostring(). Python .

+1

All Articles