Setting properties of a node from a csv - Neo4j - csv

This is an example of my csv file:
_id,official_name,common_name,country,started_by,
ABO.00,Association Football Club Bournemouth,Bournemouth,England,"{""day"":NumberInt(1),""month"":NumberInt(1),""year"":NumberInt(1899)}"
AOK.00,PAE Kerkyra,Kerkyra,Greece,"{""day"":NumberInt(30),""month"":NumberInt(11),""year"":NumberInt(1968)}"
I have to import this csv into Neo4j:
LOAD CSV WITH HEADERS FROM
'file:///Z:/path/to/file/team.csv' as line
create (p:Team {_id:line._id, official_name:line.official_name, common_name:line.common_name, country:line.country, started_by_day:line.started_by.day,started_by_month:line.started_by.month,started_by_year:line.started_by.year
I get an error(Neo.ClientError.Statement.InvalidType) setting started_by.day, started_by.month, started_by.year
How can I set rightly the properties about started_by?

Format of you csv should be following:
_id,official_name,common_name,country,started_by_day,started_by_month,started_by_year
ABO.00,Association Football Club Bournemouth,Bournemouth,England,1,1,1899
Cypher:
LOAD CSV WITH HEADERS FROM 'file:///Z:/path/to/file/team.csv' as line
CREATE (p:Team {_id:line._id, official_name:line.official_name, common_name:line.common_name, country:line.country, started_by_day:line.started_by_day,started_by_month:line.started_by_month,started_by_year:line.started_by_year})

It looks like your date part in the csv file is in JSON format - don't you need to parse that first?
line.started_by
is this string
"{""day"":NumberInt(30),""month"":NumberInt(11),""year"":NumberInt(1968)}"
There is no line.started_by.day

Related

set annotated csv for grafana's influx uploader

Trying to change a csv to an "annotated csv", in order to manually upload a file into the Grafana's influx uploader.
Says "
Failed to upload the selected CSV: The CSV could not be parsed. Please make sure that the CSV was in Annotated Format
"
manual is here : https://docs.influxdata.com/influxdb/v2.0/write-data/developer-tools/csv/#csv-annotations
My (miss-annotated) csv is :
#constant measurement,datatest
#datatype dateTime:number,long,long,string
time,temp,raw,SN
1539250260,21,409,ABC3
1539250985,27,718,ABC1
1539251114,25,496,ABC2
1539251168,22,751,ABC3
1539251893,29,725,ABC1
1539252019,28,489,ABC2
1539252076,26,753,ABC3
1539252800,29,731,ABC1
1539252930,29,485,ABC2
Thanks
Try with this
#constant measurement,datatest
#datatype time,long,long,string
time,temp,raw,SN
1539250260,21,409,ABC3
1539250985,27,718,ABC1
1539251114,25,496,ABC2
1539251168,22,751,ABC3
1539251893,29,725,ABC1
1539252019,28,489,ABC2
1539252076,26,753,ABC3
1539252800,29,731,ABC1
1539252930,29,485,ABC2

Load Relations.csv file to Neo4j Database

I have a relations csv file. I have to load this relations to neo4j but i can't.I have already created my News node and this nodes have unique id.
The csv file has iStart, iRel, iEnd columns. Thanks
Sample .csv file looks like this:
iStart iRel iEnd
------------------------
114471 IS 2225
and this is my code:
LOAD CSV WITH HEADERS FROM "file:///try.csv" as input
MATCH (from:News {id: input.iStart}), (to:News {id: input.iEnd})
CREATE (from)-[:RELATION { type: input.iRel }]->(to);
Try this one:
LOAD CSV WITH HEADERS FROM 'file:///try.csv' as input
CREATE (fromnews:FromNews{id: input.iStart}),(tonews:ToNews{id: input.iEnd})
WITH fromnews,tonews
CREATE (fromnews)-[:RELATION{type: input.iRel}]->(tonews );

Neo4j CSV import datatype error

I am trying to import genealogy data into Neo4j using a CSV file. The dates are strings such as 2012 or 19860105). However, when importing, Neo4j interprets them as LongValue, creating an error.
My import statement is either
LOAD CSV WITH HEADERS FROM 'file:///Neo4jPersonNodes1.csv' AS line FIELDTERMINATOR '|'
CREATE (:Person{RN: toInteger(line[0]),fullname: line[1],surname: line[2],name: line[3],sex: line[4],union_id: toInteger(line[5]),mn: line[6],BD: line[7],BDGed: line[8],DD: line[9],DDGed: line[10],bp_id: toInteger(line[11]),dp_id: toInteger(line[12]),BP: line[13],DP: line[14],kit: line[15]})
or, adding the toString() function
LOAD CSV WITH HEADERS FROM 'file:///Neo4jPersonNodes1.csv' AS line FIELDTERMINATOR '|'
CREATE (:Person{RN: toInteger(line[0]),fullname: toString(line[1]),surname: toString(line[2]),name: toString(line[3]),sex: toString(line[4]),union_id: toInteger(line[5]),mn: toString(line[6]),BD: toString(line[7]),BDGed: toString(line[8]),DD: toString(line[9]),DDGed: toString(line[10]),bp_id: toInteger(line[11]),dp_id: toInteger(line[12]),BP: toString(line[13]),DP: toString(line[14]),kit: toString(line[15])})
A sample of the CSV is
"RN"|"fullname"|"surname"|"name"|"sex"|"union_id"|"mn"|"BD"|"BDGed"|"DD"|"DDGed"|"bp_id"|"dp_id"|"BP"|"DP"|"kit"
"5"|"Ale Harmens Slump"|"Slump"|"Ale Harmens"|"M"|"313"|"3"|"18891223"|"23 Dec 1889"|"19890111"|"11 Jan 1989"|"23"|"4552"|"Echten, Friesland, Neth."|"Sebastopol, California"|""
the error message is:
Neo4j.Driver.V1.ClientException: 'Error when pulling unconsumed
session.run records into memory in session: Expected Long(7) to be a
org.neo4j.values.storable.TextValue, but it was a
org.neo4j.values.storable.LongValue'
I'm not sure why Neo4j does not treat the numeric string as a string.
Since your CSV file has a header row (and specified WITH HEADERS), your Cypher code must treat line as a map (whose property names match all your header names) instead of as an array.
For example, instead of line[0], you must use line.RN. If you fix all the uses of line accordingly, you should no longer get such errors.

Python3 Replacing special character from .csv file after convert the same from JSON

I am trying to develop a program using Python3.6.4 which convert a JSON file into a CSV file and also we need to clean the data in the csv file. as for example:
My JSON File:
{emp:[{"Name":"Bo#b","email":"bob#gmail.com","Des":"Unknown"},
{"Name":"Martin","email":"mar#tin#gmail.com","Des":"D#eveloper"}]}
Problem 1:
After converting that into csv its creating a blank row between every 2 rows. As
**Name email Des**
[<BLANK ROW>]
Bo#b bob#gmail.com Unknown
[<BLANK ROW>]
Martin mar#tin#gmail.com D#eveloper
Problem 2:
In my code I am using emp but I need to use it dynamically.
fobj = open("D:/Users/shamiks/PycharmProjects/jsonSamle.txt")
jsonCont = fobj.read()
print(jsonCont)
fobj.close()
employee_parsed = json.loads(jsonCont)
emp_data = employee_parsed['employee']
As we will not know the structure or content of up-coming JSON file.
Problem 3:
I also need to remove all # characters from the CSV file.
For solving Problem 3, you can use .replace (https://www.tutorialspoint.com/python/string_replace.htm).
For problem 2, you can use the dictionary keys and then get the zeroth item out of it.
fobj = open("D:/Users/shamiks/PycharmProjects/jsonSamle.txt")
jsonCont = fobj.read().replace("#", "")
print(jsonCont)
fobj.close()
employee_parsed = json.loads(jsonCont)
first_key = employee_parsed.keys()[0]
emp_data = employee_parsed[first_key]
I can't solve problem 1 without more code to see how your are exporting the result. It may be that your data has newlines in it. In which case, you could add .replace("\n","") and/or .replace("\r","") after the previous replace so the line would read fobj.read().replace("#", "").replace("\n", "").replace("\r", "").

How to read in a certain csv file column (up and down) Python

I have a cvs file with data looking like:
lastname firstname id
segre alberto 14562
I want to just read in the column with the id numbers
everything I try keeps giving me the line not the column of the csv file
import csv
import operator
idgetter = operator.itemgetter(2)
with open('path/to/file') as infile:
infile.readline()
ids = [idgetter(row) for row in csv.reader(infile)]
You could use Pandas.
import pandas as pd
col = pd.read_csv('/path/to/file/')['id']
And if you want it as a list, simply list(col) will do the trick.