码迷,mamicode.com
首页 > 编程语言 > 详细

python 实现 loadrunner xml脚本格式化

时间:2015-12-15 13:58:14      阅读:483      评论:0      收藏:0      [点我收藏+]

标签:

 

 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 = i
14     if level and (not elem.tail or not elem.tail.strip()):
15         elem.tail = i
16     return elem
17 
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

 

python 实现 loadrunner xml脚本格式化

标签:

原文地址:http://www.cnblogs.com/martinsun/p/5047723.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!