38
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

javascriptで配列→csv書き出しの実現

Last updated at Posted at 2017-08-09

nodeを使ってディープラーニング用の学習データを準備するのに配列→CSVを行う必要があったので備忘録として

var fs = require('fs');
var formatCSV = '';
var testArr = [
  [0.018317, -0.019618, -0.011231, 0.999577],
  [0.018395, -0.019801, -0.011154, 0.999573],
  [0.018559, -0.019742, -0.01115, 0.999571]
];

exportCSV(testArr);

// 配列をcsvで保存するfunction
function exportCSV(content){
  for (var i = 0; i < content.length; i++) {
      var value = content[i];
   
      for (var j = 0; j < value.length; j++) { var innerValue = value[j]===null?'':value[j].toString(); var result = innerValue.replace(/"/g, '""'); if (result.search(/("|,|\n)/g) >= 0)
      result = '"' + result + '"';
      if (j > 0)
      formatCSV += ',';
      formatCSV += result;
    }
    formatCSV += '\n';
  }
  fs.writeFile('formList.csv', formatCSV, 'utf8', function (err) {
    if (err) {
      console.log('保存できませんでした');
    } else {
      console.log('保存できました');
    }
  });
}

単純に行っており、もっといい方法があると思うので教えていただければ幸いですmm

以上

38
27
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
38
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?