将数据从neo4j导出为csv而不是json

3
我是使用neo4jdb-python包来查询Neo4j数据库的。例如,考虑下面的代码:
import neo4j
connection = neo4j.connect("http://localhost:7474")
cursor = connection.cursor()
for i in cursor.execute("MATCH a RETURN a LIMIT 1"):
    print i 

但输出的形式是元组,即

({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'},)

如何以csv格式获取输出。这是通过neo4j的Web视图实现的,输出格式如下:

"{""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""}"

然而,我希望通过客户端程序来完成,因为我需要将其嵌入到另一个程序中。如果使用neo4jdb-python不可行,则还有哪些其他选项可用。


你想把键作为表头,值作为列对吧? - Padraic Cunningham
@Padraic 更具体地说,我希望输出为"{""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""}" - Bhargav Rao
所以你想要 JSON?CSV 什么时候起作用了? - Nicole White
@NicoleWhite 当你从Neo4j Web视图中导出为“CSV”时,你会得到这个输出。JSON O/P相当奇怪(对于这个特定查询而言,超过1000个字符,是CSV大小的10倍)。 - Bhargav Rao
当我从浏览器导出csv时,我得到的是csv而不是json。你按哪个按钮?你能提供一张截图吗? - Nicole White
2个回答

6
Neo4j服务器仅返回JSON格式,正如Mark Needham在他的答案中提到的那样。
因此,将其转换为CSV的任何代码都必须在客户端完成。这可以使用csv模块完成。请注意,neo4jdb-python包仅与Python2.7兼容。
获取数据的最小代码为:
import neo4j
connection = neo4j.connect("http://localhost:7474")
cursor = connection.cursor()
data = list(cursor.execute("MATCH a RETURN a LIMIT 1")

请注意,如问题中所述,返回的值是以元组形式呈现的。创建CSV文件的最小代码为:
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()])

代码的解释很简单,打开一个文件。使用 csv.writer 创建一个 writer 对象。首先使用 writerow 写入标题。最后循环遍历字典并写入行。
获得的输出为:
"{""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""}"

这与使用exportable.coffee脚本得到的结果类似。

3

那个CSV实际上并不来自特定的API - 它是在客户端转换成CSV格式。

如果您想查看适当的代码,它位于exportable.coffee中:

    $scope.exportCSV = (data) ->
      return unless data
      csv = new CSV.Serializer()
      csv.columns(data.columns())
      for row in data.rows()
        csv.append(row)

这是关于CSV.coffee的内容。我猜你可以在Python中做类似的事情,例如使用json.dumps,像这样:

> 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"}]'

谢谢,但是有没有直接以CSV格式导出的方法,至少在Java或其他语言中? - Bhargav Rao
不,我不这么认为。据我所知,Neo4j服务器只返回JSON格式的数据。但将其转换为CSV格式应该不难,对吧? - Mark Needham
2
只需使用opencsv并通过要导出到json的节点属性返回从数据库获取的内容。这里有一些代码可供参考:https://github.com/jexp/neo4j-shell-tools/blob/master/src/main/java/org/neo4j/shell/tools/imp/format/CsvFormat.java#L39 - Michael Hunger

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接