Neo4j expand array elements into separate rows - csv
I have a CSV file that has columns that contain arrays. The file looks like this:
siblings
[3010,3011,3012]
[2010,2012]
What I am trying to do is to return the elements of the arrays as separate rows.
I looked through the documentation and found UNWIND function. When I tried the example shown in the documentation:
UNWIND [1, 2, 3] AS x
RETURN x
everything worked fine. When I tried it with my data the query looked like this:
LOAD CSV WITH HEADERS FROM
'file:///test.csv' AS line1
UNWIND line1.siblings as a
RETURN a
and the result was
[3010,3011,3012]
[2010,2012]
instead of:
3010
3011
3012
...
Does anyone know what I am doing wrong?
The CSV columns are all handled as strings so you need to treat them as such. In your case you can remove the brackets from the siblings column prior to splitting the string into a collection. That step would help you to avoid pre-processing the file. Once you split the string then you still have a collection of strings.
WITH "[1001,1002,1003]" as numbers_with_brackets_in_a_string
WITH substring( numbers_with_brackets_in_a_string, 1, size(numbers_with_brackets_in_a_string)-2 ) as numbers_in_a_string
UNWIND split( numbers_in_a_string, ',' ) as number_as_a_string
RETURN number_as_a_string
I have found an ugly way of fixing this.
I did the following thing:
LOAD CSV WITH HEADERS FROM
'file:///test.csv' AS line1
WITH line1, split(line1.siblings, ",") as s
UNWIND s as sib
RETURN sib
after which I got the following thing:
[3010
3011
3012]
...
I removed [] from the CSV file and I got the output that I desired.
I know this is an ugly solutions and I would appreciate if someone could find a better one.
Related
Reading a .dat file in Julia, issues with variable delimeter spacing
I am having issues reading a .dat file into a dataframe. I think the issue is with the delimiter. I have included a screen shot of what the data in the file looks like below. My best guess is that it is tab delimited between columns and then new-line delimited between rows. I have tried reading in the data with the following commands: df = CSV.File("FORCECHAIN00046.dat"; header=false) |> DataFrame! df = CSV.File("FORCECHAIN00046.dat"; header=false, delim = ' ') |> DataFrame! My result either way is just a DataFrame with only one column including all the data frome each column concatenated into one string. I tried to even specify the types with the following code: df = CSV.File("FORCECHAIN00046.dat"; types=[Float64,Float64,Float64,Float64, Float64,Float64,Float64,Float64,Float64,Float64,Float64,Float64]) |> DataFrame! And I received an the following error: ┌ Warning: 2; something went wrong trying to determine row positions for multithreading; it'd be very helpful if you could open an issue at https://github.com/JuliaData/CS V.jl/issues so package authors can investigate I can work around this by uploading it into google sheets and then downloading a csv, but I would like to find a way to make the original .dat file work.
Part of the issue here is that .dat is not a proper file format—it's just something that seems to be written out in a somewhat human-readable format with columns of numbers separated by variable numbers of spaces so that the numbers line up when you look at them in an editor. Google Sheets has a lot of clever tricks built in to "do what you want" for all kinds of ill-defined data files, so I'm not too surprised that it manages to parse this. The CSV package on the other hand supports using a single character as a delimiter or even a multi-character string, but not a variable number of spaces like this. Possible solutions: if the files aren't too big, you could easily roll your own parser that splits each line and then builds a matrix you can also pre-process the file turning multiple spaces into single spaces That's probably the easiest way to do this and here's some Julia code (untested since you didn't provide test data) that will open your file and convert it to a more reasonable format: function dat2csv(dat_path::AbstractString, csv_path::AbstractString) open(csv_path, write=true) do io for line in eachline(dat_path) join(io, split(line), ',') println(io) end end return csv_path end function dat2csv(dat_path::AbstractString) base, ext = splitext(dat_path) ext == ".dat" || throw(ArgumentError("file name doesn't end with `.dat`")) return dat2csv(dat_path, "$base.csv") end You would call this function as dat2csv("FORCECHAIN00046.dat") and it would create the file FORCECHAIN00046.csv, which would be a proper CSV file using commas as delimiters. That won't work well if the files contain any values with commas in them, but it looks like they are just numbers, in which case it should be fine. So you can use this function to convert the files to proper CSV and then load that file with the CSV package. A little explanation of the code: the two-argument dat2csv method opens csv_path for writing and then calls eachline on dat_path to read one line form it at a time eachline strips any trailing newline from each line, so each line will be bunch of numbers separated by whitespace with some leading and/or trailing whitespace split(line) does the default splitting of line which splits it on whitespace, dropping any empty values—this leaves just the non-whitespace entries as strings in an array join(io, split(line), ',') joins the strings in the array together, separated by the , character and writes that to the io write handle for csv_path println(io) writes a newline after that—otherwise everything would just end up on a single very long line the one-argument dat2csv method calls splitext to split the file name into a base name and an extension, checking that the extension is the expected .dat and calling the two-argument version with the .dat replaced by .csv
Try using the readdlm function in DelimitedFiles library, and convert to DataFrame afterwards: using DelimitedFiles, DataFrames df = DataFrame(readdlm("FORCECHAIN00046.dat"), :auto)
NETLOGO: Reading a csv file exported from matlab and store it as a list of lists
I am trying to read a csv file exported from matlab which contains 35040 rows and 5 columns (each element of this file is a number). After that I want to store such file as a list of lists in Netlogo. The way I am trying to do this is: globals[mylist] set mylist (csv:from-file "output.csv" ) show mylist This code actually reads the csv file and saves it as a list of lists of the kind: [[a1 a2 a3 a4 a5] [b1 b2 b3 b4 b5]] The problem is that the last element of each nested list, is stored with a series of semicolons at the end. For example in the first nested list the last element should be 0.7980 but it is stored as "0.7980;;;;;;;;;;;" so as a string. How can I solve it? Is it a problem related with the csv file I am reading or is it a problem with the code I am using? What should I do?
Yes, the problem is with your CSV file and, depending on where it comes from, the best solution might be to fix it at the source. That being said, you could also process it within NetLogo in a way that gets rid of the semicolons. Here is an example of how you could do this: to demo let list-of-lists [[1 2 3 4 "5;;;;"] [6 7 8 9 "10;;;;;"]] let new-list-of-lists map [ xs -> map parse xs ] list-of-lists print word "Old list: " list-of-lists print word "New list: " new-list-of-lists end to-report parse [ value ] report ifelse-value (is-string? value and (position ";" value != false)) [ ; if the value is a string containing a ";", take the string ; up to the position of the ";" and try to convert it to a number read-from-string substring value 0 position ";" value ] [ ; otherwise, leave the value alone value ] end This is not the most robust code in the world, but if your file follows a regular format, it could work. If not, you can always adapt it for your specific case. Aside from map, the key primitives used here are position and read-from-string. If you look them up in the dictionary, you should be able to figure out how it works...
Read a log file in R
I'm trying to read a log file in R. It looks like an extract from a JSON file to me, but when trying to read it using jsonlite I get the following error message: "Error: parse error: trailing garbage". Here is how my log file look like: {"date":"2017-05-11T04:37:15.587Z","userId":"admin","module":"Quote","action":"CreateQuote","identifier":"-.admin1002"}, {"date":"2017-05-11T05:12:24.939Z","userId":"a145fhyy","module":"Quote","action":"Call","identifier":"RunUY"}, {"date":"2017-05-11T05:12:28.174Z","userId":"a145fhyy","license":"named","usage":"External","module":"Catalog","action":"OpenCatalog","identifier":"wks.klu"}, Has you can see, the column name is precised directly in front of the content for each line (e.g: "date": or "action":) And some line can skip some columns and add some other. What I want to get as output would be to have 7 columns with the corresponding data filled in each: date userId license usage module action identifier Does anyone has a suggestion about how to get there? Thanks a lot in advance ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Thanks everyone for your answers. Here are some precisions about my issue: The data that I gave as example in an extract of one of my log files. I've got a lot of them that I need to read as one unique table. I haven't added any commas or anything to it. #r2evans I've tried the following: Log3 <-read.table("/Projects/data/analytics.log.agregated.2017-05-11.log") jsonlite::stream_in(textConnection(gsub(",$","",Log3))) It returns the following error: Error: lexical error: invalid char in json text. c(17, 18, 19, 20, 21, 22, 23, 2 (right here) ------^ I'm not sure how to use sed -e 's/,$//g' infile > outfile and Sys.which("sed"), that something I'm not familiar with. I'm looking into it, but if you have anymore precisions to give me about the usage of it that would be great.
I have saved your example as a file "test.json" and was able to read and parse it like this: library(jsonlite) rf <- read_file("test.json") rfr <- gsub("\\},", "\\}", rf) data <- stream_in(textConnection(rfr)) It parses and simplifies into a neat data frame exactly like you want. What I do is look for "}," rather than ",$", because the very last comma is not (necessarily) followed by a newline character(s). However, this might not be the best solution for very large files.. For those you may need to first look for a way to modify the text file itself by getting rid of the commas. Or, if that's possible, ask the people who exported this file to export it in a normal ndjson format:-)
Selectively Import only Json data in txt file into R.
I have 3 questions I would like to ask as I am relatively new to both R and Json format. I read quite a bit of things but I don't quite understand still. 1:) Can R parse Json data when the txt file contains other irrelevant information as well? Assuming I can't, I uploaded the text file into R and did some cleaning up. So that it will be easier to read the file. require(plyr) require(rjson) small.f.2 <- subset(small.f.1, ! V1 %in% c("Level_Index:", "Feature_Type:", "Goals:", "Move_Count:")) small.f.3 <- small.f.2[,-1] This would give me a single column with all the json data in each line. I tried to write new .txt file . write.table(small.f.3, file="small clean.txt", row.names = FALSE) json_data <- fromJSON(file="small.clean") The problem was it only converted 'x' (first row) into a character and ignored everything else. I imagined it was the problem with "x" so I took that out from the .txt file and ran it again. json_data <- fromJSON(file="small clean copy.txt") small <- fromJSON(paste(readLines("small clean copy.txt"), collapse="")) Both time worked and I manage to create a list. But it only takes the data from the first row and ignore the rest. This leads to my second question. I tried this.. small <- fromJSON(paste(readLines("small clean copy.txt"), collapse=",")) Error in fromJSON(paste(readLines("small clean copy.txt"), collapse = ",")) : unexpected character ',' 2.) How can I extract the rest of the rows in the .txt file? 3.) Is it possible for R to read the Json data from one row, and extract only the nested data that I need, and subsequently go on to the next row, like a loop? For example, in each array, I am only interested in the Action vectors and the State Feature vectors, but I am not interested in the rest of the data. If I can somehow extract only the information I need before moving on to the next array, than I can save a lot of memory space. I validated the array online. But the .txt file is not json formatted. Only within each array. I hope this make sense. Each row is a nested array. The data looks something like this. I have about 65 rows (nested arrays) in total. {"NonlightningIndices":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],"LightningIndices":[],"SelectedAction":12,"State":{"Features":{"Data":[21.0,58.0,0.599999964237213,12.0,9.0,3.0,1.0,0.0,11.0,2.0,1.0,0.0,0.0,0.0,0.0]}},"Actions":[{"Features":{"Data":[4.0,4.0,1.0,1.0,0.0,3.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.12213890532609,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.13055793241076,0.0,0.0,0.0,0.0,0.0,0.231325346416068,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.949158357257511,0.0,0.0,0.0,0.0,0.0,0.369666537828737,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0851765937900996,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.223409208023677,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.698640447815897,1.69496718435102,0.0,0.0,0.0,0.0,1.42312654023416,0.0,0.38394999584831,0.0,0.0,0.0,0.0,1.0,1.22164326251584,1.30980246401454,1.00411570750454,0.0,0.0,0.0,1.44306759429513,0.0,0.00568191150434618,0.0,0.0,0.0,0.0,0.0,0.0,0.157705869690127,0.0,0.0,0.0,0.0,0.102089274086033,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.37039305683305,2.64354332879095,0.0,0.456876463171171,0.0,0.0,0.208651305680117,0.0,0.0,0.0,0.0,0.0,2.0,0.0,3.46713142511126,2.26785558685153,0.284845692694476,0.29200364444299,0.0,0.562185300773834,1.79134869431988,0.423426746571872,0.0,0.0,0.0,0.0,5.06772310533214,0.0,1.95593334724537,2.08448537685298,1.22045520912269,0.251119892385839,0.0,4.86192274732091,0.0,0.186941346075472,0.0,0.0,0.0,0.0,4.37998688020614,0.0,3.04406665275463,1.0,0.49469909818283,0.0,0.0,1.57589195190525,0.0,0.0,0.0,0.0,0.0,0.0,3.55229001446173]}},...... {"NonlightningIndices":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,24],"LightningIndices":[[15,16,17,18,19,20,21,22,23]],"SelectedAction":15,"State":{"Features":{"Data":[20.0,53.0,0.0,11.0,10.0,2.0,1.0,0.0,12.0,2.0,1.0,0.0,0.0,1.0,0.0]}},"Actions":[{"Features":{"Data":[4.0,4.0,1.0,1.0,0.0,3.0,1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.110686363475575,0.0,0.0,0.0,0.0,0.0,0.0,0.0,1.13427913742728,0.0,0.0,0.0,0.0,0.0,0.218834141070836,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.939443046803111,0.0,0.0,0.0,0.0,0.0,0.357568892126985,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0889329732996782,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.22521492930721,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.700441220022084,1.6762090551226,0.0,0.0,0.0,0.0,1.44526456614638,0.0,0.383689214317325,0.0,0.0,0.0,0.0,1.0,1.22583659574753,1.31795156033445,0.99710368703165,0.0,0.0,0.0,1.44325394830013,0.0,0.00418600599483917,0.0,0.0,0.0,0.0,0.0,0.0,0.157518319482216,0.0,0.0,0.0,0.0,0.110244186273209,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.369899973785845,2.55505143302811,0.0,0.463342609296841,0.0,0.0,0.226088384842823,0.0,0.0,0.0,0.0,0.0,2.0,0.0,3.47842109127488,2.38476342332125,0.0698115810371108,0.276804206873942,0.0,1.53514282355593,1.77391161515718,0.421465101754304,0.0,0.0,0.0,0.0,4.45530484778828,0.0,1.43798302409155,3.46965807176681,0.468528940277049,0.259853183829217,0.0,4.86988325473155,0.0,0.190659677933533,0.0,0.0,0.963116148760181,0.0,4.29930830894124,0.0,2.56201697590845,0.593423384852181,0.46165947868584,0.0,0.0,1.59497392171253,0.0,0.0,0.0,0.0,0.0368838512398189,0.0,4.24538684327048]}},...... I would really appreciate any advice here.
Write data to CSV file from swi-prolog code
How can I write data to CSV file from the prolog code below? Thanks, SB run:- member(A,[a,b,c,d,e]), member(B,[1,2,3,4,5,6]), write(A),write(' '),write(B),nl, fail. run.
Simple solution Since you are using SWI-Prolog, you can use the CSV library. ?- use_module(library(csv)). ?- findall(row(A,B), (member(A, [a,b,c,d,e]), member(B, [1,2,3,4,5])), Rows), csv_write_file('output.csv', Rows). As you can see, I do this in two steps: I create terms of the form row(A, B). Then I hand those terms to csv_write_file/2 which takes care of creating a syntactically correct output file. Non-standard separators In your question you are not writing a comma between A and B but a space. If you really want to use the space as a separator you can set this as an option: csv_write_file('output.csv', Rows, [option(separator(0' )]) 'Unbalanced' arguments Also, in your question you have more values for B than for A. You can write code that handles this, but there are several ways in which this can be dealt with. E.g., (1) you can fill missing cells with nill; (2) you can throw an exception if same_length(As, Bs) fails; (3) you can only write the 'full' rows: length(As0, N1), length(Bs0, N2), N is max(N1, N2), length(As, N), append(As, _, As0), length(Bs, N), append(Bs, _, Bs0),