XML的缩进

从另外一个模块里边提供了缩进功能:

from xml.dom import minidom

def prettify(root):

        rough_string=ET.tostring(root,'utf-8')

        reparsed=minidom.parseString(rough_string)

        return reparsed.toprettyxml(indent="\t")

new_str=prettify(root)

f=open('new_out.xml','w')

f.write(new_str)

f.close()

print(new_str)

注意这个代码可以更牛一点的:

def MyWrite(root,file_path):

  rough_string=ET.tostring(root,'utf-8')

  reparsed=minidom.parseString(rough_string)

      new_str= reparsed.toprettyxml(indent="\t")

      f=open(file_path,'w')

      f.write(new_str)

      f.close()

 

posted @ 2017-02-26 13:23  关泉珍  阅读(84)  评论(0)    收藏  举报