【问题标题】:How to get a dictionary or list from <class 'py2neo.database.Record'>如何从 <class 'py2neo.database.Record'> 获取字典或列表
【发布时间】:2016-05-18 09:35:47
【问题描述】:

使用 py2neo 连接 neo4j 数据库: 如何将“类'py2neo.database.Record'”转换为python中的字典或列表?

【问题讨论】:

  • 您使用的是哪个版本的 py2neo?您的查询返回什么?

标签: neo4j py2neo


【解决方案1】:

您可以直接将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
  • @NigelSmall 如果您使用带有光标的 py2neo3,您会怎么做?
  • 这只是创建一个包含单个项目的列表。那个项目就是记录
【解决方案2】:

似乎任何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)]

【讨论】:

    猜你喜欢
    • 2020-03-26
    • 1970-01-01
    • 2022-12-07
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 2018-05-13
    • 1970-01-01
    • 2021-11-02
    相关资源
    最近更新 更多