我试图在CSV文件中逐行读取并将每行添加到数组中,我的代码到目前为止是:
var csvFile = 'Domain.csv';
var fs = require('fs');
var readableStream = fs.createReadStream(csvFile);
var data = '';
readableStream.on('data', function(chunk) {
var temp = '';
temp = chunk;
global.qArray.push(temp);
});
readableStream.on('end', function() {
console.log(global.qArray);
});
这个日志:
[ <Buffer 22 54 79 70 65 20 28 53 74 72 69 6e 67 29 22 2c 22 44 6f 6d 61 69 6e 20 28 53 74 72 69 6e 67 29 22 2c 22 41 76 65 72 61 67 65 49 6d 70 72 65 73 73 69 ... > ]
到控制台。我假设这意味着我的读流不是逐行接收块。谁能告诉我怎样才能让它单独读取每一行?
如果你走得更轻松,应该会更轻松。
var fs = require('fs'),
readline = require('readline');
var rd = readline.createInterface({
input: fs.createReadStream('Domain.csv'),
output: process.stdout,
terminal: false
});
rd.on('line', function(line) {
console.log(line);
});