Search code examples
pythonneo4jpy2neo

Node object is not iterable


I get this error message, when I try to parse the result set, returned by MATCH query. What I want is to somehow convert the resultset to a dictionary. I should say that I know how to access particular fields of the result set - like row['a']['name'], but what I do not like is that I can not convert the whole row['a'] to a dictionary or to get something like row['a'].keys().

So, this is what I tried:

res = graph.cypher.execute("MATCH (a:Object {id: 1}) return a")
for r in res:
    print r['a']['id'] # this works

for r in res:
    print r['a'].keys() # this does not

#what I want is something like
{x:y for (x,y) in zip(r['a'].keys(), r['a'].values()}

Solution

  • From the documentation, it looks like execute is returning a py2neo.cypher.RecordList of py2neo.cypher.Record objects, which can then be iterated over:

    for r in res:
        for v in r['a']:
            # do something with v
    

    Unfortunately, looking at the source code, there doesn't seem to be an obvious way to access the column name, without doing a dir(r) and filtering the results, e.g. [c for c in dir(r) if not c.startswith('_')].

    Edit: Looking at it again, I guess r is the Record while r['a'] is something else. You'll have to see what kinda of object r['a'] is using type(r['a']), and then see if there's a way to access the keys.