这里的通用解决方案,让说,如果2个XML树是数除特定属性外,其他内容完全相同
import xml.etree.ElementTree as ET
xml1 = '<?xml version="1.0" encoding="utf-8" ?><Math mode="inline" tau="tex" xml:id="foo"><XMath>2x+3c</XMath></Math>'
xml2 = '<Math mode="inline" tau="tex" xml:id="bar"><XMath>2x+3c</XMath></Math>'
#see for more informations https://docs.python.org/3.4/library/xml.etree.elementtree.html
def almost_equals(tree1, tree2, attributes_to_ignore):
""" Return true or false depending on the fact that tree1 and tree2 are identical except for the attributes whose the tag is in attributes to ignore. """
#remove attributes to ignore
for attribute in attributes_to_ignore:
try:
tree1.attrib.__delitem__(attribute)
except:
pass
try:
tree2.attrib.__delitem__(attribute)
except:
pass
#compare nodes
if tree1.tag != tree2.tag:
print(tree1.tag,"!=",tree2.tag)
return False
if tree1.attrib != tree2.attrib:
print(tree1.attrib,"!=",tree2.attrib)
return False
if tree1.text != tree2.text:
print(tree1.text,"!=",tree2.text)
return False
subtrees1 = list(tree1)
subtrees2 = list(tree2)
if len(subtrees1) != len(subtrees2):
return False
result = True
for i in range(len(subtrees1)):
result = result and almost_equals(subtrees1[i], subtrees2[i], attributes_to_ignore)
return result
if __name__ == "__main__":
xmlTree1 = ET.fromstring(xml1)
xmlTree2 = ET.fromstring(xml2)
print("The 2 xml trees are identical ({0})".format(almost_equals(xmlTree1, xmlTree2, ["{http://www.w3.org/XML/1998/namespace}id"])))
希望它有帮助。 亚瑟。
编辑:您可以将XML保存为xml并根据需要解析它们,或者节省由内置python库生成的Element对象。
你能提供一个**文本**例子和一些实际的代码吗?通常,对象必须实现'__hash__'和'__eq__'作为字典键。 – jonrsharpe 2014-11-04 12:27:39
并非所有内容都可以是字典键,请阅读http://www.tutorialspoint.com/python/python_dictionary.htm搜索页面中的“限制”。 – 2014-11-04 14:08:58