2017-12-20 1 views
1

Hallo ich habe ein Problem mit CSV-Datei in JavaScript zu analysieren. Struktur CSV unterSlice String von CSV JavaScript

<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> 

dieses CSV zu analysieren, I-Code verwenden, unter

<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> 

Das Problem ist, wenn ich innerhalb Text Komma habe ich haben Array wie diese

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

Meine Frage ist, , wie man dies in ein Array wie dieses umwandelt:

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

Vielen Dank für Ihre Hilfe!

+4

Mögliche Duplikat [Wie kann ich eine CSV-Zeichenfolge mit Javascript analysieren, das Komma in Daten enthalten?] (Https://stackoverflow.com/questions/8493195/how-can- i-parse-a-csv-zeichenfolge-with-javascript-which-enthält-comma-in-data) – str

+0

Hat Ihre CSV konsistent nur 3 Spalten? –

+0

Ja, ich habe 3 Spalten, aber in der Textspalte kann mehr Koma sein – breakbit

Antwort

0

Die folgende rekursive Funktion wird Ihnen helfen, hängt aber davon ab, wie viele Spalten Sie pro Zeile genau extrahieren müssen.

/* $.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);

0

Beispiel mit regulären Ausdrücken. Wahrscheinlich möchten Sie die Array-Länge überprüfen und eine Fehlerbehandlung durchführen, falls keine Übereinstimmung auftritt. Verlassen Sie sich darauf, dass die CSV die einzigen drei Spalten ist und dass die letzte Spalte die einzigen enthält, die zusätzliche Kommas enthalten. Ein sauberer Weg vielleicht, um Ihre Kommas an der Quelle wenn möglich zu entkommen.

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);