Search code examples
node.jsevent-stream

How do I read csv file line by line, modify each line, write result to another file


I recently used event-stream library for nodejs to parse a huge csv file, saving results to database. How do I solve the task of not just reading a file, but modifying each line, writing result to new file? Is it some combination of through and map method, or duplex? Any help is highly appreciated.


Solution

  • If you use event-stream for read you can use split() method process csv line by line. Then change and write line to new writable stream.

    var fs = require('fs');
    var es = require('event-stream');
    
    const newCsv = fs.createWriteStream('new.csv');
    
    fs.createReadStream('old.csv')
      .pipe(es.split())
      .pipe(
        es.mapSync(function(line) {
        // modify line way you want
          newCsv.write(line);
        }))
    
    newCsv.end();