I am parsing a bunch of csv files with node using node-csv. I have hundreds of files that need to be parsed, however, I need to add a header row to each file to make use of the 'columns' option in the parser. The columns option parses each row as an object with the header row serving as object keys, however for this option to work, you of course need a header row.
By looking at the docs, my gut tells me I should be able to pipe my node stream through a transform function that adds the row, but I'm having a difficult time making this work without altering the existing data in the file.
Here is what I'm thinking, but how do I write a row to the 'zero' column?
let createStream = function() {
let stream = fs.createReadStream(transPath+'/'+file, {encoding: 'utf8'});
let parser = csv.parse({columns: true});
let transform = csv.transform(function(record, doneTransform){
//check if first zero row,
//Add header column.
doneTransform();
});
return stream.pipe(transform).pipe(parser);
};
createStream().on('data', function(transaction){
//do stuff with object data
});