我有一个xml文件,其结构如下:
<main_tag>
<first>
<tag1>val1</tag1>
<conf>
<tag2>val2</tag2>
<tag3>val3</tag3>
<tag4>val4</tag4>
</conf>
</first>
<second>
<tag1>val2</tag1>
<conf>
<tag2>val6</tag2>
<tag3>val7</tag3>
<tag4>val8</tag4>
</conf>
</second>
</main_tag>
我必须更改tag2的值。可能的值存储在dict中:
{tag2values:[newvalue1, newvalue2]}
如果tag1的值是val1,那么我们将tag2的值改为newvalue1。如果tag1值为val2,则将tag2值更改为newvalue2。
所以问题是:有没有办法在lxml中找到与其父元素值匹配的元素?
或者根据元素的父邻居值来查找元素?
最佳答案
.xpath
method让我们通过XPath 1.0表达式查找标记:
>>> from lxml import etree
>>> from cStringIO import StringIO
>>> tag2values = ['newvalue1', 'newvalue2']
>>> example = StringIO("""\
... <main_tag>
... <first>
... <tag1>val1</tag1>
... <conf>
... <tag2>val2</tag2>
... <tag3>val3</tag3>
... <tag4>val4</tag4>
... </conf>
... </first>
... <second>
... <tag1>val2</tag1>
... <conf>
... <tag2>val6</tag2>
... <tag3>val7</tag3>
... <tag4>val8</tag4>
... </conf>
... </second>
... </main_tag>
... """)
>>> tree = etree.parse(example)
>>> value1selector = '*/conf/tag2[../../tag1/text() = "val1"]'
>>> value2selector = '*/conf/tag2[../../tag1/text() = "val2"]'
>>> for elem in tree.xpath(value1selector):
... elem.text = tag2values[0]
...
>>> for elem in tree.xpath(value2selector):
... elem.text = tag2values[1]
...
>>> print(etree.tostring(tree, pretty_print=True))
<main_tag>
<first>
<tag1>val1</tag1>
<conf>
<tag2>newvalue1</tag2>
<tag3>val3</tag3>
<tag4>val4</tag4>
</conf>
</first>
<second>
<tag1>val2</tag1>
<conf>
<tag2>newvalue2</tag2>
<tag3>val7</tag3>
<tag4>val8</tag4>
</conf>
</second>
</main_tag>
在上面的示例中,
value1selector
中的XPath表达式将所有tag2
元素(它们都是conf
的子元素)作为ElementTreetag1
实例提供给您,并使用一个带textval1
的兄弟标记作为ElementTreeElement
实例,从而使替换它们的文本内容变得很简单。关于python - 如何在python和lxml中查找具有某些值的标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11061823/