2017-12-20 6 views
1

こんにちは私はJavaScriptでCSVファイルを解析する際に問題があります。このCSVを解析するために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> 

以下の構造CSV私は、私は、テキスト内のコンマているときに問題が、ある

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

以下のコードを使用して、私はこの

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

ような配列を持っている私の質問は、これをこのような配列に変換する方法:

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

ありがとうございました!

+4

の可能性のある重複した[どのようにデータにカンマが含まれているJavaScriptを使用してCSV文字列を、解析することができますか?](https://stackoverflow.com/questions/8493195/how-can- i-parse-a-csv-string-javascript-with-comma-in-data) – str

+0

あなたのCSVは一貫して3列しか持っていませんか? –

+0

はい私は3列を持っていますが、テキスト列にはもっと昏睡状態になることがあります – breakbit

答えて

0

次の再帰関数は役立ちますが、行ごとに抽出する必要がある列の数を正確に把握しているかどうかによって異なります。正規表現を使用して

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

例。おそらく、配列の長さをチェックして、何の不一致も起こらないようなエラー処理をしたいと思うでしょう。唯一の3つの列であるCSVに依存し、最後の列はカンマを追加する唯一の列です。可能であれば、ソースでカンマをエスケープするためのよりクリーンな方法です。

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

関連する問題