1 import re 2 3 from lxml import etree 4 5 def indent(elem, level=0): 6 i = "\n" + level*"\t" 7 if len(elem): 8 if not elem.text or not elem.text.strip(): 9 elem.text = i + "\t"10 for e in elem:11 indent(e, level+1)12 if not e.tail or not e.tail.strip():13 e.tail = i14 if level and (not elem.tail or not elem.tail.strip()):15 elem.tail = i16 return elem17 18 19 file_name = 'lr_xml.txt'20 21 elems_root = etree.parse(file_name).getroot()22 final_elem=indent(elems_root)23 init_content=etree.tostring(final_elem, encoding="utf-8", method="xml")24 25 26 27 # 每行左对齐28 regex1 = re.compile("\t")29 convert_first= re.sub(regex1,"",init_content)30 31 # 每行开始加上"32 regex2=re.compile("^<",flags=re.M)33 convert_second = re.sub(regex2,"\"<",convert_first)34 35 #每行末尾加上"36 regex3 = re.compile(">$", flags=re.M)37 final_content=re.sub(regex3,">\"",convert_second)38 39 print final_content