xml转成特定格式

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')
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。