Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python lxml节点比较_Python_Lxml - Fatal编程技术网

Python lxml节点比较

Python lxml节点比较,python,lxml,Python,Lxml,是否有方法检查节点是否与lxml库相等?例如,在php DOMDocument中有isSameNode: a->isSameNode(b) //return boolean 我需要它来做这样的事情: def find_special_node(special_node, xml): #iterating nodes in xml if node == special_node: return node 不确定是否要检查NodeA是否为NodeB或Node

是否有方法检查节点是否与lxml库相等?例如,在php DOMDocument中有
isSameNode

a->isSameNode(b) //return boolean
我需要它来做这样的事情:

def find_special_node(special_node, xml):
    #iterating nodes in xml
    if node == special_node:
        return node

不确定是否要检查
NodeA是否为NodeB
NodeA==NodeB

你可以尝试:

for node in X.iter():
    if node == special_node: # maybe "node is special_node" ?
        return node

无论如何,既然您已经有了
特殊节点
,为什么要搜索它呢?

不确定是否要检查
NodeA是NodeB
还是
NodeA==NodeB

你可以尝试:

for node in X.iter():
    if node == special_node: # maybe "node is special_node" ?
        return node

无论如何,既然您已经有了
special\u node
,为什么要搜索它呢?

您可以使用xpath来查找项目,在find()中使用“special\u node”的属性作为所需的属性(和值)。如果两个节点的所有属性都相同(例如类型、值、大小等),则我假设两个节点被确定为相同的
只是猜测

我的实施:

import lxml.etree as etree
def isSameNode(a,b,tagsame=True):
    for attrib in a.attrib:
        if a.get(attrib) != b.get(attrib):
            print a.get(attrib),b.get(attrib)
            return False
        else:
            return False
    if a.text != b.text:
        return False
    if tagsame==True:
        if a.tag != b.tag:
            return False
    if a.prefix != b.prefix:
        return False
    if a.tail != b.tail:
        return False
    if a.values()!=b.values(): #may be redundant to the attrib matching
        return False
    if a.keys() != b.keys(): #may also be redundant to the attrib matching
        return False
    return True

def find_alike(xml,special_element,tagsame=True):
    tree = etree.fromstring(xml)
    for node in tree.iter():
        if isSameNode(node,special_element,tagsame):
            return node
    return None

您可以使用xpath来查找项,在find()中使用“special_node”的属性作为必需的属性(和值)。如果两个节点的所有属性都相同(例如类型、值、大小等),则我假设两个节点被确定为相同的
只是猜测

我的实施:

import lxml.etree as etree
def isSameNode(a,b,tagsame=True):
    for attrib in a.attrib:
        if a.get(attrib) != b.get(attrib):
            print a.get(attrib),b.get(attrib)
            return False
        else:
            return False
    if a.text != b.text:
        return False
    if tagsame==True:
        if a.tag != b.tag:
            return False
    if a.prefix != b.prefix:
        return False
    if a.tail != b.tail:
        return False
    if a.values()!=b.values(): #may be redundant to the attrib matching
        return False
    if a.keys() != b.keys(): #may also be redundant to the attrib matching
        return False
    return True

def find_alike(xml,special_element,tagsame=True):
    tree = etree.fromstring(xml)
    for node in tree.iter():
        if isSameNode(node,special_element,tagsame):
            return node
    return None

例如,我需要检查我的
特殊节点是否在
xml
中。但无论如何,我需要知道是否有类似的php
isSameNode
节点==special_node
节点是special_node
都不工作,这可能是因为内存位置不同。(只是猜测。)这就是我的实现所做的。>>>我>>>找到相似的(s,i)#s是我的xml字符串。例如,我需要检查我的
特殊节点是否在
xml
中。但无论如何,我需要知道是否有类似的php
isSameNode
节点==special_node
节点是special_node
都不工作,这可能是因为内存位置不同。(只是猜测。)这就是我的实现所做的。>>>我>>>找到相似的(s,i)#s是我的xml字符串。