3

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' ]
4
  • 1
    So your file is broken, it should be \r\n not just \r; which it is as evident by your own test there. Commented Oct 4, 2012 at 22:23
  • The library (and your data.split()) look for either \r\n (carriage return, newline) or \n (newline). However your csv file is using \r as the line separator. This is incorrect; make whatever is writing your csv file use \r\n or \n. Commented Oct 5, 2012 at 15:55
  • 1
    \r was 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. Commented Dec 31, 2012 at 3:26
  • @Chad This was the answer to my question as well. Thank you! Commented Oct 11, 2022 at 10:17

1 Answer 1

2

\r\n, \n, and \r are all valid line endings, so you need to be prepared to split on all of them.

Convert them all to a common value before doing the split. Something like:

var lines = data.replace(/\r\n?/g, "\n").split("\n");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.