Pretty printing XML in Python

后端 未结 26 1741
一个人的身影
一个人的身影 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 03:04

    As of Python 3.9 (still a release candidate as of 12 Aug 2020), there is a new xml.etree.ElementTree.indent() function for pretty-printing XML trees.

    Sample usage:

    import xml.etree.ElementTree as ET
    
    element = ET.XML("text")
    ET.indent(element)
    

    The upside is that it does not require any additional libraries. For more information check https://bugs.python.org/issue14465 and https://github.com/python/cpython/pull/15200

提交回复
热议问题