这是我的xml:
<request><table attributeA="50" attributeB="1"></table>........</request>
如何更新attributeA的值,以获得类似attributeA =“456”
的内容<request><table attributeA="456" attributeB="1"></table>........</request>
答案 0 :(得分:2)
使用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>