這裏的通用解決方案,讓說,如果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