I am using py2neo and I would like to extract the information from query returns so that I can do stuff with it in python. For example, I have a DB containing three "Person" nodes:
for num in graph.cypher.execute("MATCH (p:Person) RETURN count(*)"):
print num
outputs:
>> count(*)
3
Sorry for shitty formatting, it looks essentially the same as a mysql output. However, I would like to use the number 3 for computations, but it has type py2neo.cypher.core.Record
. How can I convert this to a python int so that I can use it? In a more general sense, how should I go about processing cypher queries so that the data I get back can be used in Python?
graph.cypher.execute()
returns aRecordList
containing multipleRecords
. EachRecord
corresponds to a single line of the result of your Cypher query.Your
RETURN count(*)
query returns only one line, so thefor num in ...
loop will only touch oneRecord
in yourRecordList
.To get data from the columns of a record, you can use indices or column names:
This should be an
int
, but you can now convert it to whatever you need withfloat()
orint()
.Your query returns only one line with one column. You can shorten it to:
The first
[0]
gets the firstRecord
of your resultingRecordList
and the second[0]
gets the first column of theRecord
.Have a look at: http://py2neo.org/2.0/cypher.html#records
can you int(), float() str() on the
__str__()
method that looks to be outputting the value you want in your example?