格式化(美化)xml
  • 用 xml.dom.minidom
from xml.dom import minidom
prettyXmlString = minidom.parseString(ET.tostring(root)).toprettyxml(indent='\t')
  • 用 BeautifulSoup
from bs4 import BeautifulSoup
prettyXmlString = BeautifulSoup(xxx, "xml").prettify()
清理空行
prettyXmlString = '\n'.join([line for line in parseString(data).toprettyxml(indent='\t').split('\n') if line.strip()])

*** walker的流水账 ***