簡體   English   中英

通過lxml python在xml中更改屬性值

[英]changing attribute value in xml via lxml python

這是我的xml:

<request><table attributeA="50" attributeB="1"></table>........</request>

如何更新attributeA的值,使其具有諸如attributeA =“ 456”之類的內容

<request><table attributeA="456" attributeB="1"></table>........</request>

使用etree和xpath:

>>> from lxml import etree
>>> xml = '<request><table attributeA="50" attributeB="1"></table></request>'
>>> root = etree.fromstring(xml)
>>> for el in root.xpath("//table[@attributeA]"):
...     el.attrib['attributeA'] = "456"
...
>>> print etree.tostring(root)
<request><table attributeA="456" attributeB="1"/></request>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM