我对两个 xml 元素的等价性感兴趣;而且我发现测试元素的 tostring 有效;但是,这似乎很老套。
有没有更好的方法来测试两个 etree 元素的等价性?
直接比较元素:
import xml.etree.ElementTree as etree
h1 = etree.Element('hat',{'color':'red'})
h2 = etree.Element('hat',{'color':'red'})
h1 == h2 # False
将元素作为字符串进行比较:
etree.tostring(h1) == etree.tostring(h2) # True
原文由 oneporter 发布,翻译遵循 CC BY-SA 4.0 许可协议
这个比较功能对我有用: