Creating a neo4j chart database from a csv file using py2neo

I am currently working in a doctoral program, and I'm interested in Py2neo, so I use it to perform some experiments using social graphs. However, I got into problems with beginners. Excuse me for asking these simple questions.

I got an xml dataset containing jornal publication data, I converted it to a csv table, there are about 700 entries, and each entry consists of four fields: date, title, keywords, author. So my first question is how to program the chart from this table. I thought about writing a python script that loops the csv table, reads fields for each row and columns, and writes them to nodes. ++++++++++++++++++++++++++++++++++++++++++++++++ Code ++++ +++++++++++++++++++++++++++++++++++++++++

#!/usr/bin/env python # import csv from py2neo import neo4j, cypher from py2neo import node, rel # calls database service of Neo4j # graph_db = neo4j.GraphDatabaseService("http://localhost:7474/db/data/") # # Create nodes and relationships from a csv table # since it a csv table, a reader must be invoked ifile = open('testeout5_cp.csv', "rb") reader = csv.reader(ifile) # clear database graph_db.clear() rownum = 0 for row in reader: colnum = 0 for col in row: titulo, autor, rel = graph_db.create( {"titulo": col[1]}, {"autor": col[3]}, (1, "eh_autor_de", 0) ) print(titulo, autor) rownum += 1 ifile.close() 

================ I got this output (Fragment): Python 2.7.5 (default, August 22, 2013, 09:31:58) [GCC 4.8.1 20130603 (Red Hat 4.8.1-1)] on aires2, Standard

  (Node('http://localhost:7474/db/data/node/10392'), Node('http://localhost:7474/db/data /node/10393')) (Node('http://localhost:7474/db/data/node/10394'), Node('http://localhost:7474/db/data/node/10395')) (Node('http://localhost:7474/db/data/node/10396'), Node('http://localhost:7474/db/data/node/10397')) (Node('http://localhost:7474/db/data/node/10398'), Node('http://localhost:7474/db/data/node/10399')) (Node('http://localhost:7474/db/data/node/10400'), Node('http://localhost:7474/db/data/node/10401')) (Node('http://localhost:7474/db/data/node/10402'), Node('http://localhost:7474/db/data/node/10403')) (Node('http://localhost:7474/db/data/node/10404'), Node('http://localhost:7474/db/data/node/10405')) 

========= What is wrong?

+4
source share
2 answers

I am not a py2neo expert, so I can't help it. However, you tried to use a different mechanism to create your schedule? Since it is not very large, I would think about using a spreadsheet (I use it a lot) - it is dead easily.

For more information see http://blog.neo4j.org/2013/03/importing-data-into-neo4j-spreadsheet.html .

Hope this makes sense.

Rik

0
source

I think there is nothing wrong, your code looks good.

You print the nodes and get the correct py2neo node instances. Try print(titulo, autor, rel) to see if your relationship is created.

Just check with webinterface at http://localhost:7474/webadmin/ if your data is there. Since you do not have too many nodes, you can try a simple cypher query to get all the nodes and check if everything is ok.

 START n=node(*) RETURN n; 
0
source

Source: https://habr.com/ru/post/1502192/


All Articles