【发布时间】:2016-05-18 09:35:47
【问题描述】:
使用 py2neo 连接 neo4j 数据库: 如何将“类'py2neo.database.Record'”转换为python中的字典或列表?
【问题讨论】:
-
您使用的是哪个版本的 py2neo?您的查询返回什么?
使用 py2neo 连接 neo4j 数据库: 如何将“类'py2neo.database.Record'”转换为python中的字典或列表?
【问题讨论】:
您可以直接将Record 转换为列表:
result = graph.cypher.execute('MATCH (n) RETURN n')
a_record = result[0] # -> this is a Record object
list_of_things_in_record = list(a_record)
print(list_of_things_in_record)
【讨论】:
dict - 您可以在函数中包装Record。
似乎任何neo4j.data.Record 中的data()-Method 都可以将其转换为dict。
with self.driver.session() as session:
records = session.write_transaction(self._return_nodes, cypher)
rdict = [rec.data() for rec in records]
return rdict
@staticmethod
def _return_nodes(tx, cypher):
return [rec for rec in tx.run(cypher)]
【讨论】: