Export data from neo4j to csv instead of json
I am using packages neo4jdb-python
to query a Neo4j database. For example, given the below code
import neo4j
connection = neo4j.connect("http://localhost:7474")
cursor = connection.cursor()
for i in cursor.execute("MATCH a RETURN a LIMIT 1"):
print i
But the output is in the form of a tuple. those.
({u'text': u'Stoyanov, S., Hoogveld, B., Kirschner, P.A., (2010). Mapping Major Changes to Education and Training in 2025, in JRC Technical Note JRC59079., Publications Office of the European Union: Luxembourg.', u'identifier': u'reference/lak/226'},)
How to get output in csv format. This is possible with the neo4j web view. and the way out is like
"{""text"":""Stoyanov, S., Hoogveld, B., Kirschner, P.A., (2010). Mapping Major Changes to Education and Training in 2025, in JRC Technical Note JRC59079., Publications Office of the European Union: Luxembourg."",""identifier"":""reference/lak/226""}"
However, I want to do this through a client program as I need to embed it in another program. If this is not possible with neo4jdb-python
, what other options are available.
source to share
This CSV doesn't actually come from a specific API - it gets translated to CSV format on the client side.
The relevant code is in exportable.coffee if you want to take a look:
$scope.exportCSV = (data) ->
return unless data
csv = new CSV.Serializer()
csv.columns(data.columns())
for row in data.rows()
csv.append(row)
And that goes for CSV.coffee . I think you should be able to do something like this in Python, perhaps with json.dumps like this:
> import json
> t = ({u'text': u'Stoyanov, S., Hoogveld, B., Kirschner, P.A., (2010). Mapping Major Changes to Education and Training in 2025, in JRC Technical Note JRC59079., Publications Office of the European Union: Luxembourg.', u'identifier': u'reference/lak/226'},)
> json.dumps(t)
'[{"text": "Stoyanov, S., Hoogveld, B., Kirschner, P.A., (2010). Mapping Major Changes to Education and Training in 2025, in JRC Technical Note JRC59079., Publications Office of the European Union: Luxembourg.", "identifier": "reference/lak/226"}]'
source to share
The Neo4j server only returns JSON as Mark Needham says in his response .
Hence, any code that needs to be converted to CSV must be client-side. This can be done using the csv module . Please note that the package is neo4jdb-python
only compatible with Python2.7.
Minimum code to get data
import neo4j
connection = neo4j.connect("http://localhost:7474")
cursor = connection.cursor()
data = list(cursor.execute("MATCH a RETURN a LIMIT 1")
Note that, as mentioned in the question, the return values ββare in the form of tuples. The minimal code to create a csv file is
with open("test.csv","w") as csvfile:
writer = csv.writer(csvfile,delimiter = ',',quotechar = '"',quoting = csv.QUOTE_ALL)
writer.writerow(t[0].keys())
for i in t:
writer.writerow(['{"%s":"%s"}'%(k,v) for k,v in i.iteritems()])
The explanation of the code is simple, open the file. Using csv.writer
, create an object writer
. First, write a title using writerow
. Finally, loop through the dictionary and write the lines.
The resulting result
"{""text"":""Stoyanov, S., Hoogveld, B., Kirschner, P.A., (2010). Mapping Major Changes to Education and Training in 2025, in JRC Technical Note JRC59079., Publications Office of the European Union: Luxembourg.""}","{""identifier"":""reference/lak/226""}"
which is similar to the one obtained using the exportable.coffee
script.
source to share