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.
Asked
Active
Viewed 1,443 times
1 Answers
3
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();

l2ysho
- 2,542
- 1
- 20
- 41
-
Thanks, just was not sure where to plug the write stream in. – Dominik Domanski Jun 13 '19 at 04:59