xml转成特定格式
2021-12-30 本文已影响0人
闪电恋
xml文件 --xml_bw.txt
<king>1不错</king>
<new>2</new>
<test></test>
<tests>22</tests>
目标格式
king:://king::xpath/jpath::RESPONSE
new:://new::xpath/jpath::RESPONSE
tests:://tests::xpath/jpath::RESPONSE
Python脚本实现:
import re
def xml_dict(xml_file):
f=open(xml_file,'rb')
#xml的内容转成str
s =f.read().decode('utf-8')
# 提取value
a =re.findall(r'[>](.*?)[<]',s)
# 提取key
b =re.findall(r'[<](.*?)[>]',s)
# print(b)
dict={} #创建空dict存值
i=j=0
while i<len(b):
# print(b[i])
dict.update({b[i]:a[j]})
i+=2
j+=1
return dict
dict1=xml_dict('xml_bw.txt')
for (key,value) in dict1.items():
# print(dict1[key])
if dict1[key]:
print(key+':://'+key+'::xpath/jpath::RESPONSE')