在JavaScript中解析csv,每行数据量不清楚

时间:2017-08-04 15:26:38

标签: javascript html csv

我尝试用html文件解析JavaScript中的csv文件,如下所示:

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

我尝试了一些像http://papaparse.com/这样的东西和库,但我有点担心一个值的多个数据(并不总是相同的数量),一个记录有多个条目。

1 个答案:

答案 0 :(得分:2)

CSV的RFC规范不允许使用可变记录长度。

RFC 4180 CSV format section 2.4

要么修复输出CSV的代码(如果你可以控制它,这是理想的解决方案),那么它尊重规范,或者你可以使用split()来做一个快速和脏的解析器。

请注意,如果你选择DIY路线,并且你已经在解析非标准的CSV,那么你可能会遇到各种各样的陷阱。例如,必须处理可能需要包含逗号/分号的值的双引号,或者首先输出的任何不符合规范的行为。