Python 获取ElementTree中的最后一个标记并追加文本
我有一些具有以下结构的XML:Python 获取ElementTree中的最后一个标记并追加文本,python,xml-parsing,elementtree,xml.etree,Python,Xml Parsing,Elementtree,Xml.etree,我有一些具有以下结构的XML: <root> <parent-1> <text>blah-1</text> <properties> <property type="R" id="0005">text-value-A</property> <prop
<root>
<parent-1>
<text>blah-1</text>
<properties>
<property type="R" id="0005">text-value-A</property>
<property type="W" id="0003">text-value-B</property>
<property type="H" id="0002">text-value-C</property>
<property type="W" id="0008">text-value-D</property>
</properties>
</parent-1>
<parent-2>
<text>blah-2</text>
<properties>
<property type="W" id="0004">text-value-A</property>
<property type="H" id="0087">text-value-B</property>
</properties>
</parent-2>
<parent-3>
<text>blah-3</text>
<properties>
<property type="H" id="0087">text-value-C</property>
<property type="R" id="0008">text-value-A</property>
</properties>
</parent-3>
<parent-4>
<text>blah-4</text>
<properties>
<property type="H" id="0019">text-value-C</property>
<property type="R" id="0060">text-value-A</property>
</properties>
</parent-4>
</root>
鉴于上述所需的输出,我想知道我是否需要一种不同的方法来解决这个问题,或者类似于以下内容的概念将以某种方式起作用:
alist = []
for item in root.findall('parent/properties/property'):
alist.append(item.text)
for element in alist:
if element in alist[-1]:
self.alist = '&'.join([a for b,a in enumerate(alist) if a not in alist[:b]]
if not element in alist[-1]:
self.alist = '!'.join([a for b,a in enumerate(alist) if a not in alist[:b]]
谢谢这可能就是你想要的
- xpath公式“//properties”生成一个包含四个元素的列表
将包含每个属性的文本列表属性_文本
谓词用于测试当前属性的文本集以前是否见过。如果没有,则这些文本将作为列表添加到集合中。(重要的是要使用any
逻辑,以避免在不同的顺序中丢失重复的集合。)set
它确实产生了这种输出
text-value-A!text-value-B!text-value-C!text-value-D&text-value-A!text-value-B&text-value-C!text-value-A
这样做的一个固有问题是xml子项没有排序。在您的示例中,我看到
属性
元素是按id
属性排序的。可以用这个吗?很好,但这应该是随机的,抱歉-现在已经编辑好了。至少我还不清楚。也许您可以告诉我们该输入的输出是什么样子的。顺便说一句,若要亲自回复某人,请键入“at”字符以接收可能的收件人的菜单。@BillBell给定上述XML,输出应为以下字符串:text-value-a!text-value-B!text-value-C!text-value-D&text-value-A!text-value-B和text-value-C!text-value-A
。它可以工作。感谢您抽出时间,并感谢您的详细解释。
from xml.etree import ElementTree
tree = ElementTree.parse('bt123.xml')
property_text_lists = []
for properties in tree.findall('.//properties'):
property_texts = [p.text for p in properties]
if any([set(property_texts)==set(ptl) for ptl in property_text_lists]):
break
property_text_lists.append(property_texts)
print ('&'.join(['!'.join(property_text_lists[i]) for i in range(len(property_text_lists))]))
text-value-A!text-value-B!text-value-C!text-value-D&text-value-A!text-value-B&text-value-C!text-value-A