I have the following node.js code:
new lazy(fs.createReadStream('file.csv'))
.lines
.forEach(function(line){
console.log(line.toString());
});
However, I only get the last line of data this way. The contents of the csv file are as follows:
123,broken
12345,stolen
1234567,lost
What am I doing wrong here?
I also have this code for the same file:
fs.readFile(req.files.file.path, 'utf8', function (err, data) {
if (err) throw err;
var lines = data.split(/\r?\n/);
console.log(lines);
});
Which returns the following array:
[ '123,broken\r12345,stolen\r1234567,lost' ]
\r\nnot just\r; which it is as evident by your own test there.data.split()) look for either\r\n(carriage return, newline) or\n(newline). However your csv file is using\ras the line separator. This is incorrect; make whatever is writing your csv file use\r\nor\n.\rwas the only valid line separator on MacOS before OS X so decent plain text parsing solutions ought to be engineered to deal with it, as annoying as it may be.