Pretty printing XML in Python

后端 未结 26 1753
一个人的身影
一个人的身影 2020-11-22 02:18

What is the best way (or are the various ways) to pretty print XML in Python?

26条回答
  •  梦如初夏
    2020-11-22 02:50

    from yattag import indent
    
    pretty_string = indent(ugly_string)
    

    It won't add spaces or newlines inside text nodes, unless you ask for it with:

    indent(mystring, indent_text = True)
    

    You can specify what the indentation unit should be and what the newline should look like.

    pretty_xml_string = indent(
        ugly_xml_string,
        indentation = '    ',
        newline = '\r\n'
    )
    

    The doc is on http://www.yattag.org homepage.

提交回复
热议问题