如何用 python (xml.etree.ElementTree) 寻址下一个 iter?
How to address the next iter with python (xml.etree.ElementTree)?
<storage>
<record>
<values>
<points>99999999</points>
<points>Mr</points>
<points>Marvin</points>
<points>Homes</points>
<points>hardware</points>
<points>true</points>
<points>de</points>
<points>6</points>
<points>false</points>
</values>
</record>
</storage>
您好,
我正在尝试使用 python ( xml.etree.ElementTree ) 更改一些 xml 值。
这是xml数据的一小部分。
appelation=re.compile("Mr")
for fname in root.iter('points'):
if appelation.match(str(pTest)):
fname.text="New Mr/Mrs"
## here i am trying to edit the next iter (<points>Marvin</points>)
##fname.next().text="New name" -> doesnt work
关于如何处理下一个 iter 有什么建议吗?
xml 文件有很多名为 <"points"> 的标签,并且值总是不同的。
我假设您正在使用 xml.etree.ElementTree
,因为它是标准库的一部分。考虑以下片段:
appelation = re.compile('Mr')
points = root.iter('points')
for node in points:
if appelation.match(node.text):
node.text = 'Monsieur'
node = next(points)
node.text = 'Francois'
break
ElementTree.dump(根)
在这个片段中,points
是一个迭代器,我们用它来获取下一个点和搜索。一旦我们找到了我们正在寻找的节点 (Mr),我们就可以对该节点和下一个节点做一些事情(通过在所述可迭代对象上调用 next
)。
输出:
<storage>
<record>
<values>
<points>99999999</points>
<points>Monsieur</points>
<points>Francois</points>
<points>Homes</points>
<points>hardware</points>
<points>true</points>
<points>de</points>
<points>6</points>
<points>false</points>
</values>
</record>
</storage>
更新
如果要修改本节点、下一个节点、上一个节点;那么您需要跟踪前一个节点,因为可迭代对象无法返回。最简单的方法是使用堆栈(list
或 collections.deque
即可):
appelation = re.compile('Mr')
points = root.iter('points')
nodes_stack = []
for node in points:
if appelation.match(node.text):
# Modify this node
node.text = 'Monsieur'
# Modify next node
next_node = next(points)
next_node.text = 'Francois'
# Modify previous node
previous_node = nodes_stack.pop()
previous_node.text = 'modified'
# Keep popping the stack the get to previous nodes
# in reversed order
ElementTree.dump(root)
break
else:
nodes_stack.append(node)
<storage>
<record>
<values>
<points>99999999</points>
<points>Mr</points>
<points>Marvin</points>
<points>Homes</points>
<points>hardware</points>
<points>true</points>
<points>de</points>
<points>6</points>
<points>false</points>
</values>
</record>
</storage>
您好,
我正在尝试使用 python ( xml.etree.ElementTree ) 更改一些 xml 值。 这是xml数据的一小部分。
appelation=re.compile("Mr")
for fname in root.iter('points'):
if appelation.match(str(pTest)):
fname.text="New Mr/Mrs"
## here i am trying to edit the next iter (<points>Marvin</points>)
##fname.next().text="New name" -> doesnt work
关于如何处理下一个 iter 有什么建议吗? xml 文件有很多名为 <"points"> 的标签,并且值总是不同的。
我假设您正在使用 xml.etree.ElementTree
,因为它是标准库的一部分。考虑以下片段:
appelation = re.compile('Mr')
points = root.iter('points')
for node in points:
if appelation.match(node.text):
node.text = 'Monsieur'
node = next(points)
node.text = 'Francois'
break
ElementTree.dump(根)
在这个片段中,points
是一个迭代器,我们用它来获取下一个点和搜索。一旦我们找到了我们正在寻找的节点 (Mr),我们就可以对该节点和下一个节点做一些事情(通过在所述可迭代对象上调用 next
)。
输出:
<storage>
<record>
<values>
<points>99999999</points>
<points>Monsieur</points>
<points>Francois</points>
<points>Homes</points>
<points>hardware</points>
<points>true</points>
<points>de</points>
<points>6</points>
<points>false</points>
</values>
</record>
</storage>
更新
如果要修改本节点、下一个节点、上一个节点;那么您需要跟踪前一个节点,因为可迭代对象无法返回。最简单的方法是使用堆栈(list
或 collections.deque
即可):
appelation = re.compile('Mr')
points = root.iter('points')
nodes_stack = []
for node in points:
if appelation.match(node.text):
# Modify this node
node.text = 'Monsieur'
# Modify next node
next_node = next(points)
next_node.text = 'Francois'
# Modify previous node
previous_node = nodes_stack.pop()
previous_node.text = 'modified'
# Keep popping the stack the get to previous nodes
# in reversed order
ElementTree.dump(root)
break
else:
nodes_stack.append(node)