Pretty printing XML in Python

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

    You can try this variation...

    Install BeautifulSoup and the backend lxml (parser) libraries:

    user$ pip3 install lxml bs4
    

    Process your XML document:

    from bs4 import BeautifulSoup
    
    with open('/path/to/file.xml', 'r') as doc: 
        for line in doc: 
            print(BeautifulSoup(line, 'lxml-xml').prettify())  
    

提交回复
热议问题