Pretty printing XML in Python

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

    BeautifulSoup has a easy to use prettify() method.

    It indents one space per indentation level. It works much better than lxml's pretty_print and is short and sweet.

    from bs4 import BeautifulSoup
    
    bs = BeautifulSoup(open(xml_file), 'xml')
    print bs.prettify()
    

提交回复
热议问题