返回None而不是String

时间:2020-04-27 12:06:11

标签: python recursion

我写了一个似乎可以正常工作的方法来递归地遍历xml。但是它返回None而不是字符串。我发现它与我调用递归函数的方式有关,但无法弄清楚如何使其起作用。 print(“ returning:{}”。format(path)打印正确的结果,但返回值为None。

这是我的代码:

  # retrieves the path of an given xml-node
def getParentValueAndId(self,node, path):
    actual_node = node.getparent().getparent().getparent().find(self.openEHR_prefix + "rm_attribute_name")
    node_id = node.getparent().getparent().find(self.openEHR_prefix + "node_id").text
    if (path == "value" and node_id is not None):
        path = "/{}[{}]/{}".format(actual_node.text, node_id, path)
        try:
            self.getParentValueAndId(actual_node, path)
        except:
            pass
    elif not (node_id is None):
        path = "/{}[{}]{}".format(actual_node.text, node_id, path)
        try:
            self.getParentValueAndId(actual_node, path)
        except:
            print("returning: {}".format(path))
            return path
    else:
        try:
            self.getParentValueAndId(actual_node, path)
        except:
            pass

0 个答案:

没有答案
相关问题