我有一个xml结构,其中一些元素不是唯一的。所以我设法对子树进行排序,我可以过滤多次使用的元素。但删除功能似乎不适用。
我的XML结构看起来像这样简化:
<root>
<page>
<text>blabla blub unique</text>
<text>blabla blub not unique</text>
<text>blabla blub not unique</text><!-- line should be removed -->
<text>blabla blub not unique</text><!-- line should be removed -->
<text>blabla blub not unique</text><!-- line should be removed -->
<text>blabla blub again unique</text>
</page>
<page>
<text>2nd blabla blub unique</text>
<text>2nd blabla blub not unique</text>
<text>2nd blabla blub not unique</text><!-- line should be removed -->
<text>2nd blabla blub again unique</text>
</page>
</root>
我想在每个页面上删除双字符串,所以我在页面中迭代两个for循环中的页面和元素:(重要行的提取,我希望没有忘记任何事情)
import xml.etree.ElementTree as ET
self.tree = ET.parse(path)
self.root = self.tree.getroot()
self.prev = None
# [...]
for page in self.root: # iterate over pages
for elem in page:
if elements_equal(elem, self.prev):
print("found duplicate: %s" % elem.text) # equal function works well
page.remove(elem) # <---- removes just one line
continue
self.prev = elem
# [...]
self.tree.write("out.xml") # 2 duplicate lines still there....
更新:代码似乎有效,但它只删除一个重复,而不是全部
答案 0 :(得分:3)
我不知道你是如何定义的elements_equal
,但是(无耻地改编自Testing Equivalence of xml.etree.ElementTree)这对我有用:
编辑:在迭代page
时存储要删除的每个元素的列表,然后删除它们,而不是在一个循环内执行删除。
编辑:在元素标记的比较中注意到代码中的一个小错误并更正它。
import xml.etree.ElementTree as ET
path = 'in.xml'
tree = ET.parse(path)
root = tree.getroot()
prev = None
def elements_equal(e1, e2):
if type(e1) != type(e2):
return False
if e1.tag != e2.tag: return False
if e1.text != e2.text: return False
if e1.tail != e2.tail: return False
if e1.attrib != e2.attrib: return False
if len(e1) != len(e2): return False
return all([elements_equal(c1, c2) for c1, c2 in zip(e1, e2)])
for page in root: # iterate over pages
elems_to_remove = []
for elem in page:
if elements_equal(elem, prev):
print("found duplicate: %s" % elem.text) # equal function works well
elems_to_remove.append(elem)
continue
prev = elem
for elem_to_remove in elems_to_remove:
page.remove(elem_to_remove)
# [...]
tree.write("out.xml")
给出:
$ python undupe.py
found duplicate: blabla blub not unique
found duplicate: 2nd blabla blub not unique
$ cat out.xml
<root>
<page>
<text>blabla blub unique</text>
<text>blabla blub not unique</text>
<text>blabla blub again unique</text>
</page>
<page>
<text>2nd blabla blub unique</text>
<text>2nd blabla blub not unique</text>
<text>2nd blabla blub again unique</text>
</page>