Python如何优雅的格式化XML 【Python XML Format】

格式化xml需要用到库:xml.dom.minidom

直接上代码:

#coding:utf-8
import xml.dom.minidom
uglyxml = 'Leonardo DiCaprio'
xml = xml.dom.minidom.parseString(uglyxml)
xml_pretty_str = xml.toprettyxml()
print xml_pretty_str

XML格式化结果:

<?xml version="1.0" ?>
<employees>
  <employee>
    <Name>Leonardo DiCaprio</Name>
  </employee>
</employees>

你可能感兴趣的:(python,xml,dom,python)