0

I try to parse a csv file in JavaScript in a html file which looks like this:

Task,started at,Done by,Start stem,top measurement,bottom measurement,time,west measurement,east measurement,time,Measured height difference,time,Actual adjustment,time,Horizontal measured offset,time,Actual adjustment,time,finished at,finished by
task;01,startedAt;8/4/2017 @ 10:58:19,doneBy;ernie,startStem;8/4/2017 @ 10:58:24,topMeasurement;0.2,bottomMeasurement;0.1,timeTB;8/4/2017 @ 10:58:39,westMeasurement;0.2,eastMeasurement;0.1,timeWE;8/4/2017 @ 10:58:44,topMeasurement;0.2,bottomMeasurement;0.1,timeTB;8/4/2017 @ 10:58:46,measuredHeightDifference;0.22,timeHE;8/4/2017 @ 10:58:59,actualAdjustmentHE;43,timeAHE;8/4/2017 @ 10:59:1,measuredHeightDifference;0.7,timeHE;8/4/2017 @ 10:59:6,actualAdjustmentHE;34,timeAHE;8/4/2017 @ 10:59:8,measuredHeightDifference;0.7,timeHE;8/4/2017 @ 10:59:13,horizontalMeasuredOffset;0.8,timeHO;8/4/2017 @ 10:59:20,actualAdjustmentHO;56,timeAHO;8/4/2017 @ 10:59:22,horizontalMeasuredOffset;0.2,timeHO;8/4/2017 @ 10:59:28,actualAdjustmentHO;23,timeAHO;8/4/2017 @ 10:59:30,horizontalMeasuredOffset;0.1,timeHO;8/4/2017 @ 10:59:34,actualAdjustmentHO;3,timeAHO;8/4/2017 @ 10:59:37,horizontalMeasuredOffset;0.1,timeHO;8/4/2017 @ 10:59:40,finishedAt;8/4/2017 @ 10:59:56,finishedBy;ernie
task;01,startedAt;8/4/2017 @ 11:0:31,doneBy;bert,startStem;8/4/2017 @ 11:0:35,topMeasurement;3,bottomMeasurement;4,timeTB;8/4/2017 @ 11:0:50,westMeasurement;3,eastMeasurement;4,timeWE;8/4/2017 @ 11:0:53,topMeasurement;5,bottomMeasurement;3,timeTB;8/4/2017 @ 11:0:56,westMeasurement;5,eastMeasurement;3,timeWE;8/4/2017 @ 11:1:0,topMeasurement;5,bottomMeasurement;3,timeTB;8/4/2017 @ 11:1:3,measuredHeightDifference;2,timeHE;8/4/2017 @ 11:1:15,actualAdjustmentHE;3,timeAHE;8/4/2017 @ 11:1:16,measuredHeightDifference;4,timeHE;8/4/2017 @ 11:1:20,actualAdjustmentHE;2,timeAHE;8/4/2017 @ 11:1:22,measuredHeightDifference;1,timeHE;8/4/2017 @ 11:1:25,actualAdjustmentHE;4,timeAHE;8/4/2017 @ 11:1:26,measuredHeightDifference;1,timeHE;8/4/2017 @ 11:1:29,horizontalMeasuredOffset;4,timeHO;8/4/2017 @ 11:1:35,actualAdjustmentHO;3,timeAHO;8/4/2017 @ 11:1:36,horizontalMeasuredOffset;4,timeHO;8/4/2017 @ 11:1:40,finishedAt;8/4/2017 @ 11:1:57,finishedBy;bert
task;01,startedAt;8/4/2017 @ 11:2:22,doneBy;bernie,startStem;8/4/2017 @ 11:2:27,topMeasurement;0.3,bottomMeasurement;0.7,timeTB;8/4/2017 @ 11:2:43,westMeasurement;0.3,eastMeasurement;0.7,timeWE;8/4/2017 @ 11:2:49,topMeasurement;0.3,bottomMeasurement;0.7,timeTB;8/4/2017 @ 11:2:51,measuredHeightDifference;4,timeHE;8/4/2017 @ 11:2:59,actualAdjustmentHE;34,timeAHE;8/4/2017 @ 11:3:1,measuredHeightDifference;5,timeHE;8/4/2017 @ 11:3:3,actualAdjustmentHE;345,timeAHE;8/4/2017 @ 11:3:5,measuredHeightDifference;5,timeHE;8/4/2017 @ 11:3:9,horizontalMeasuredOffset;4,timeHO;8/4/2017 @ 11:3:13,actualAdjustmentHO;234,timeAHO;8/4/2017 @ 11:3:15,horizontalMeasuredOffset;4,timeHO;8/4/2017 @ 11:3:18,finishedAt;8/4/2017 @ 11:3:35,finishedBy;bernie

I tried a few things and libraries like http://papaparse.com/ but I´m little bit stuck with the problem of having multiple data (and not always the same amount) of one value with multiple entries for one record.

3
  • 1
    It'd be easier to help if you could provide a minimal, verifiable, example. This means you should post at the very least a code sample of what you've tried, an example of the kind of data that doesn't work (which you might have provided, but it's hard to tell from looking at that unformatted csv dump), and the expected result. Commented Aug 4, 2017 at 15:32
  • I think it doesnt make sense to post the things I tried, because they didn´t realy work. The csv I´ve posted is the one I want to parse, but the main problem is that I could have multiple measurements in there per measurement type. Is it possible to post a table here to show what it should look like after parsing? I didnt find something for this Commented Aug 4, 2017 at 15:37
  • I recommend the excellent jquery-csv library: github.com/evanplaice/jquery-csv Commented Aug 4, 2017 at 15:39

1 Answer 1

2

The RFC specification for CSV does not allow for variable record lengths.

RFC 4180 CSV format section 2.4

Either you want to fix the code that outputs the CSV (if you have control over it, that's the ideal solution) so it respects the specs, or you can do a quick and dirty parser using split()s.

Mind that if you go the DIY route, and you're already parsing non-standard CSVs, you might run into all sorts of pitfalls along the way. For example having to handle double quotes for values that might need to include commas/semi-colons, or other out-of-spec behaviour from whatever's outputting that in the first place.

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

2 Comments

Agreed. It looks like OP's CSV data is invalid. There are (what appears to be) column names on each row. He will have a hard time finding a standard library that will parse this custom CSV.
I think I propably have to go all the way and adjust the data I get. I was hopping for something which may can do it, but I think its just not realy rational to try it with this output

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.