py2neo:如何迭代占用起始节点和结束节点的所有关系?

时间:2016-02-01 23:23:27

标签: graph neo4j py2neo

如何在获取起始节点和结束节点的图形上迭代所有关系?我试过:

import sys
import time
import json
from py2neo import Graph, Node, authenticate, Relationship
graph =Graph()
cypher = graph.cypher

def handle_row(row):
    a,b = row
    ... do some stuff with a,b

cypher.execute("match (a)-[]->(b) return a, b", row_handler=handle_row)

但我收到错误:

`typeError: <function handle_row at ...> is not JSON serializable`

1 个答案:

答案 0 :(得分:3)

cypher.execute()函数不将结果处理程序作为参数。它将查询参数作为字典或关键字参数。然后将这些参数作为JSON发送到neo4j。您的handle_row函数不是JSON可序列化的,因此TypeError

要对所有节点执行某些操作,请尝试以下操作:

result = graph.cypher.execute('MATCH (a)-[]->(b) RETURN a, b')
for row in result:
    print(row)
    print(row[0])
    print(row[2])

请参阅此处的示例:http://py2neo.org/2.0/cypher.html#api-cypher