在Node.js中将csv文件读入multidimensional array的确切步骤是什么?
我有一个.csv文件中的以下数据:
Smith,Tom,43 Johnson,Sue,28 Sommers,Jeff,44
我想用这个数据填充一个multidimensional array,以便:
array[0][0] == Smith array[1][2] == 28 etc.
使用csv模块 –
var csv = require('csv'); var people = []; var filePath = 'path/to/my/awesome/csv/file.csv'; csv() .from.path(filePath) .to.array(function(data) { data.forEach(function(person, index) { people[index] = []; people[index][0] = person[0]; people[index][1] = person[1]; } }
我也试过以下(使用ya-csv )并且也喜欢它:
function readFile() { var $IN = require('ya-csv'); var $filePath = 'data.csv'; var $people = []; var $reader = $IN.createCsvFileReader($filePath, { 'separator': ',' }); $reader.on('data', function(item) { $people.push(item); }) }