3

Using graph.run() py2neo v3 to connect to neo4j DB: How can I convert an instance of py2neo.database.Cursor class to a dictionary or list in python?

Was simple in py2neo v2 using py2neo.cypher.core.RecordList class which is what graph.cypher.execute() the equivalent to graph.run would have returned if using the previous version...

Tms91
  • 3,456
  • 6
  • 40
  • 74
user1613312
  • 374
  • 2
  • 15

3 Answers3

2

Looks like you could do with the data method:

http://py2neo.org/v3/database.html#py2neo.database.Cursor.data

This is designed for use with libraries like Pandas where you need to extract the entire result.

Nigel Small
  • 4,475
  • 1
  • 17
  • 15
1

As pointed out by Nigel Small, you can use the .data() method to convert it into a list.

Here is how it works,
let's take for example this line of code calling graph.run, whose result is saved into tags variable:

tags = graph.run(query)

the result is a py2neo.database.Cursor class (containing a dictionary):

 tags
-------------------------------
 ['py2neo', 'python', 'neo4j']

And by applying the .data() method, it becomes:

tags = graph.run(query).data()

whose result is a list (containing a dictionary):

[{'tags': ['py2neo', 'python', 'neo4j']}]
Tms91
  • 3,456
  • 6
  • 40
  • 74
0

You can just cast it to a list :

result = session.run("MATCH (n) RETURN n")
records = list(result)
Christophe Willemsen
  • 19,399
  • 2
  • 29
  • 36