Python 读取 XAML 文件中的所有元素并替换一些值

所以我有XML文件,在文件里面我有很多elements这样的:


<name search = "read TTT: write">

    <version id = "1.0.0">

        <value>myVal</value>

        <method>myMethod</method>

    </version>

</name>

因此,在每一个元素我需要找到这search = "read TTT: write"行,在此情况下,read TTT: write开始与read我需要改变它(用一个字符串替换读)。


最好的\最快的方法是什么?


更新


因此,如果我有element这样的情况(存在“阅读”一词):


<name search = "read TTT: write">

    <version id = "1.0.0">

        <value>myVal</value>

        <method>myMethod</method>

    </version>

</name>

我想用这个替换它(我只替换读取):


<name search = "NewValue TTT: write">

    <version id = "1.0.0">

        <value>myVal</value>

        <method>myMethod</method>

    </version>

</name>


幕布斯7119047
浏览 393回答 3
3回答

杨魅力

使用minidom:from xml.dom import minidomxmldoc = minidom.parseString(xml)tags = xmldoc.getElementsByTagName("name")firstchild = tags[0]firstchild.attributes["search"].value = 'NewValue TTT: write'print(xmldoc.toxml())输出:<?xml version="1.0" ?><name search="NewValue TTT: write">&nbsp; &nbsp; <version id="1.0.0">&nbsp; &nbsp; &nbsp; &nbsp; <value>myVal</value>&nbsp; &nbsp; &nbsp; &nbsp; <method>myMethod</method>&nbsp; &nbsp; </version></name>编辑:对于多个标签,只需循环:xml = '''\<root><name search = "read TTT: write">&nbsp; &nbsp; <version id = "1.0.0">&nbsp; &nbsp; &nbsp; &nbsp; <value>myVal</value>&nbsp; &nbsp; &nbsp; &nbsp; <method>myMethod</method>&nbsp; &nbsp; </version></name><name search = "read2 TTT: write">&nbsp; &nbsp; <version id = "2.0.0">&nbsp; &nbsp; &nbsp; &nbsp; <value>myVal_2</value>&nbsp; &nbsp; &nbsp; &nbsp; <method>myMethod_2</method>&nbsp; &nbsp; </version></name></root>'''from xml.dom import minidomxmldoc = minidom.parseString(xml)tags = xmldoc.getElementsByTagName("name")for item in tags:&nbsp; &nbsp; item.attributes["search"].value = 'NewValue TTT: write'print(xmldoc.toxml())输出:<?xml version="1.0" ?><root><name search="NewValue TTT: write">&nbsp; &nbsp; <version id="1.0.0">&nbsp; &nbsp; &nbsp; &nbsp; <value>myVal</value>&nbsp; &nbsp; &nbsp; &nbsp; <method>myMethod</method>&nbsp; &nbsp; </version></name><name search="NewValue TTT: write">&nbsp; &nbsp; <version id="2.0.0">&nbsp; &nbsp; &nbsp; &nbsp; <value>myVal_2</value>&nbsp; &nbsp; &nbsp; &nbsp; <method>myMethod_2</method>&nbsp; &nbsp; </version></name></root>使用beautifulsoup:from bs4 import BeautifulSoupxml = '''\<root><name search = "read TTT: write">&nbsp; &nbsp; <version id = "1.0.0">&nbsp; &nbsp; &nbsp; &nbsp; <value>myVal</value>&nbsp; &nbsp; &nbsp; &nbsp; <method>myMethod</method>&nbsp; &nbsp; </version></name><name search = "read TTT: write">&nbsp; &nbsp; <version id = "2.0.0">&nbsp; &nbsp; &nbsp; &nbsp; <value>myVal_2</value>&nbsp; &nbsp; &nbsp; &nbsp; <method>myMethod_2</method>&nbsp; &nbsp; </version></name></root>'''soup = BeautifulSoup(xml , 'lxml')newVal = "NewValue"for elem in soup.find_all("name"):&nbsp; &nbsp; elem['search'] = elem['search'].replace(str(elem['search']).split(" ")[0], newVal)&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print(elem)输出:<name search="NewValue TTT: write"><version id="1.0.0"><value>myVal</value><method>myMethod</method></version></name><name search="NewValue TTT: write"><version id="2.0.0"><value>myVal_2</value><method>myMethod_2</method></version></name>

慕桂英546537

from xml.dom.minidom import parse #minidom does the trickdom = parse('my_file.xml') #read in your fileres = dom.getElementsByTagName('name') #fetch the desired elements in the treefor element in res: #loop through all&nbsp;&nbsp; &nbsp; if element.getAttribute('search') == 'read TTT: write': #filter for the attribute and value&nbsp; &nbsp; &nbsp; &nbsp; element.setAttribute('search', 'New Value TTT: write') #in case of match, replacewith open('my_file_updated.xml','w') as f: #store the file&nbsp; &nbsp; f.write(dom.toxml())

万千封印

试试这个:xml = """<?xml version="1.0" encoding="UTF-8"?>&nbsp; &nbsp; &nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <name search = "read TTT: write">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version id = "1.0.0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <value>myVal</value>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <method>myMethod</method>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </name>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <name search = "read TTT: write">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version id = "1.0.0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <value>myVal</value>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <method>myMethod</method>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </name>&nbsp; &nbsp; &nbsp; &nbsp; </body>&nbsp; &nbsp; &nbsp; &nbsp; """import xml.etree.ElementTree as ETtree = ET.fromstring(xml)sh = tree.findall('name')for name in sh:&nbsp; &nbsp; val = name.get("search")&nbsp; &nbsp; if str(val) == "read TTT: write":&nbsp; &nbsp; &nbsp; &nbsp; name.set('search', 'NewValue TTT: write')print (ET.tostring(tree))输出:&nbsp; &nbsp;<body>&nbsp; &nbsp; &nbsp; &nbsp; <name search = "NewValue TTT: write">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version id = "1.0.0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <value>myVal</value>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <method>myMethod</method>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </version>&nbsp; &nbsp; &nbsp; &nbsp; </name>&nbsp; &nbsp; &nbsp; &nbsp; <name search = "NewValue TTT: write">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version id = "1.0.0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <value>myVal</value>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <method>myMethod</method>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</name>&nbsp; &nbsp; </body>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python