美文网首页
xml转成特定格式

xml转成特定格式

作者: 闪电恋 | 来源:发表于2021-12-30 22:37 被阅读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')
    

    相关文章

      网友评论

          本文标题:xml转成特定格式

          本文链接:https://www.haomeiwen.com/subject/rwmwqrtx.html