-1

I have the folllowing code which opens a directory, reads each csv file in it and creates a stream:

var files = fs.readdirSync('./dir/');
for (var i in files) {
var stream = fs.createReadStream('./dir/' + files[i]);
  stream.on('data', function(data) {
      console.log(data);
  });
}

I can see that the console.log prints out binary data that looks like so:

<Buffer 22 50 52 30 20 32 41 41 22 2c 35 30 2c 33 35 35 30 30 38 2c 34 32 39 33 37 38 2c 22 45 39 32 30 30 30 30 30 31 22>

How can I use this to parse my csv file line by line?

I've managed to get the data back using toString():

 console.log(data.toString());

but fairly sure this isn't the route I need to go.

I want to be able to extract the first entry for each line in the csv.

Mike Rifgin
  • 10,409
  • 21
  • 75
  • 111
  • Why the down vote ? You should leave a comment with reasoning. I've looked at and implenented A csv module but wanted to see if I could get this working with node. Surely there is a way to get the buffer in to a useable form ?? – Mike Rifgin Feb 12 '15 at 16:38

1 Answers1

0

This is actually the sort of thing I was looking for. And it turns out toString() is used:

https://stackoverflow.com/a/11874688/392572

Community
  • 1
  • 1
Mike Rifgin
  • 10,409
  • 21
  • 75
  • 111