2

Hi I have problem with parse CSV file in JavaScript. Structure CSV below

<code> 
date, hours, text
2004-05-04,05:22, Sample text, with coma
2005-05-04,05:22:00, Another Sample text, with coma
2006-05-04,05:22, Third Sample text, with coma
</code>

To parse this CSV I use code below

<code>
$.ajax({
        type: "GET",
        url: "data.csv",
        dataType: "text",
        success: function(data) {processData(data);}
     });
function processData(csv) {
    var allTextLines = csv.split(/\r\n|\n/);
    //console.log(allTextLines);
    var lines = [];
    while (allTextLines.length) {
        //console.log(allTextLines);
        lines.push(allTextLines.shift().split(","));
    }
    console.log(lines);
}
</code>

The problem is, when i have comma inside text I have array like this

<code>
lines[0]=['2004-05-04']['05:22'][Sample text][with comma];
</code>

My question is, how to convert this to array like this one:

<code>
lines[0]=['2004-05-04']['05:22'][Sample text, with coma];
</code>

Thanks for help!

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
breakbit
  • 37
  • 6
  • 4
    Possible duplicate of [How can I parse a CSV string with Javascript, which contains comma in data?](https://stackoverflow.com/questions/8493195/how-can-i-parse-a-csv-string-with-javascript-which-contains-comma-in-data) – str Dec 20 '17 at 17:39
  • Does your CSV consistently only have 3 columns? – Seb Cooper Dec 20 '17 at 17:44
  • Yes I have 3 column but in text column can be more coma – breakbit Dec 20 '17 at 17:48

2 Answers2

0

The following recursive function will help, but depends on knowing exactly how many columns you have to extract per row.

/* $.ajax({
            type: "GET",
            url: "data.csv",
            dataType: "text",
            success: function(data) {processData(data);}
         });*/
         
    var csvtext = "date, hours, text\n2004-05-04,05:22, Sample text, with coma\n2005-05-04,05:22:00, Another Sample text, with coma\n2006-05-04,05:22, Third Sample text, with coma";
    
    processData(csvtext);
    
    function processData(csv) {
        var allTextLines = csv.split(/\r\n|\n/);
        
        var lines = [];
        while (allTextLines.length) {
            var lineSplit = [];
            pushStrings(0, allTextLines.shift(), 0, lineSplit);
            lines.push(lineSplit);
        }
        console.log(lines);
    }

    function pushStrings(start, string, count, arr){
        if(count == 2){
            arr.push(string);
            return false;
        }    
        startIndex = string.indexOf(',');
        var el = string.substring(start, startIndex)
        stringNew = string.substring(++startIndex);
        arr.push(el);
        pushStrings(0, stringNew.trim(), ++count, arr);
    }
    console.log(lines);
Seb Cooper
  • 564
  • 2
  • 13
0

Example using regular expressions. You would probably most want to check array length and perform some error handling should no match occur. Relies on the CSV being the only three columns and the last column being the only one to contain additional commas. A cleaner way maybe to escape your commas at source if possible.

var csvtext = "date, hours, text\n2004-05-04,05:22, Sample text, with coma\n2005-05-04,05:22:00, Another Sample text, with coma\n2006-05-04,05:22, Third Sample text, with coma";

function processData(csv) {
  var allTextLines = csv.split(/\r\n|\n/);
  var lines = [];
  while (allTextLines.length) {
    var lineSplit = allTextLines.shift().match(/([^\,]+),\s?([^\,]+),\s?(.+)/i).splice(1);
    console.log(lineSplit);
    lines.push(lineSplit);
  }
  console.log(lines);
}

processData(csvtext);
Ben Morris
  • 282
  • 3
  • 13