Pretty printing XML in Python

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

    lxml is recent, updated, and includes a pretty print function

    import lxml.etree as etree
    
    x = etree.parse("filename")
    print etree.tostring(x, pretty_print=True)
    

    Check out the lxml tutorial: http://lxml.de/tutorial.html

提交回复
热议问题