alex7786
alex7786

Reputation: 3

Parse a csv in JavaScript with unclear amount of data per row

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.

Upvotes: 0

Views: 51

Answers (1)

Efulefu
Efulefu

Reputation: 46

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.

Upvotes: 2

Related Questions