我在目录中有一个Node应用程序以及一个名为 sample.csv 的文本文件。
我正在尝试使用以下代码逐行读取文件,但没有任何内容被读取:
var readline = require('readline');
var fs = require('fs');
var lineReader = readline.createInterface({
input: fs.createReadStream('sample.csv')
});
lineReader.on('line', function (line) {
console.log(line); // Never happens
});
console.log('Completed.'); // Immediately skips to this
有什么好主意吗? :)谢谢!
答案 0 :(得分:3)
试试这个:
var readline = require('linebyline'),
rl = readline('./sample.csv');
rl.on('line', function (line, lineCount, byteCount) {
console.log(lineCount, line, byteCount);
// do something with the line of text
})
.on('error', function (e) {
console.log("error", e);
// something went wrong
});
节点作弊在line_by_line处可用。