3

I'm using neo4jdb-python packages to query the Neo4j database. For example, considering 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. i.e.

({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 do I get the output in csv format.This is possible with the web view of neo4j. and the output 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 it via a client program as I need to embed it into another program. If it is not possible with neo4jdb-python, then what other options are available.

5
  • you want the keys as headers and the values as columns yes? Commented Nov 8, 2014 at 11:15
  • @Padraic to be more specific I want the output as "{""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""}" Commented Nov 8, 2014 at 11:48
  • So you want json? Where is the csv coming into play? Commented Nov 8, 2014 at 17:29
  • @NicoleWhite When you export as "CSV" from the Neo4j web view, you get that as output. The JSON O/P is quite weird (and more than 1000 characters for this particular query which is 10 times the size of CSV.) Commented Nov 8, 2014 at 18:28
  • When I export a csv from the browser, I get a csv, not json. Which button are you pressing? Can you provide a screen shot? Commented Nov 8, 2014 at 19:19

2 Answers 2

6

The Neo4j Server returns only JSON, as Mark Needham mentions in his answer.

Hence any code to do convert it to CSV must be on the client side. This can be done using the csv module. Note that neo4jdb-python package is compatible with Python2.7 only.

A minimal code for obtaining the data is

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 returned values are in the form of tuples. The minimal code to create the 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 for the code is simple, open a file. Using csv.writer, create a writer object. Write the header first using writerow. Finally loop through the dictionary and write the rows.

The output obtained is

"{""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 that obtained using the exportable.coffee script.

Sign up to request clarification or add additional context in comments.

Comments

3

That CSV isn't actually coming from a particular API - it's being translated into CSV format on the client side.

The appropriate 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 refers to CSV.coffee. I guess you should be able to do something similar in Python perhaps using 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"}]'

3 Comments

Thanks, But is there a direct way to export as CSV atleast in Java or any other language?
No I don't think so. Neo4j server only returns JSON AFAIK. It shouldn't be too hard to translate that to CSV though or?
Just use opencsv and return what you get from the database via the node properties that you want to export to json. See here for some code: github.com/jexp/neo4j-shell-tools/blob/master/src/main/java/org/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.