With nodejs I want to parse a .csv file of 10000 records and do some operation on each row. I tried using http://www.adaltas.com/projects/node-csv. I couldnt get this to pau
csv-parser instead of csv-parse used in some
of the answers above. csv-parser came around 2 years after
csv-parse. csv-parser better, as it is easy to handle headers through it.Install the csv-parser first:
npm install csv-parser
So suppose you have a csv-file like this:
NAME, AGE
Lionel Messi, 31
Andres Iniesta, 34
You can perform the required operation as:
const fs = require('fs');
const csv = require('csv-parser');
fs.createReadStream(inputFilePath)
.pipe(csv())
.on('data', function(data){
try {
console.log("Name is: "+data.NAME);
console.log("Age is: "+data.AGE);
//perform the operation
}
catch(err) {
//error handler
}
})
.on('end',function(){
//some final operation
});
For further reading refer