Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to transform input csv to output csv via pipes #28

Closed
maxfi opened this issue Apr 10, 2014 · 1 comment
Closed

How to transform input csv to output csv via pipes #28

maxfi opened this issue Apr 10, 2014 · 1 comment

Comments

@maxfi
Copy link

maxfi commented Apr 10, 2014

I'm trying to add an extra column to a csv file and would like to do this via streams utilising a pipe. This is what I have so far:

var fs = require('fs');
var csv = require("fast-csv");

var input = fs.createReadStream("input.csv");
var output = fs.createWriteStream("output.csv", {encoding: 'utf-8'});

var count = 1;

var csvStream = csv({rtrim: true, headers: true})
    .on("record", function(data){
        data.ordinalValue = count;
        count = count + 1;
     })
     .on("end", function(){
        console.log("done");
     });

input.pipe(csvStream).pipe(output);

It just puts a stringified JSON object though..

@doug-martin
Copy link
Contributor

So, we had to do something similar, and with the latest version of fast-csv this is possible.

var fs = require('fs');
var csv = require("fast-csv");
var count = 1;
var csvOutputStream = csv.createWriteStream({rtrim: true, headers: true});
var output = csvOutputStream
    .pipe(fs.createWriteStream("output.csv", {encoding: 'utf-8'}))
    .on("finish", function () {
        console.log("done");
    });

csv.fromPath("input.csv", {rtrim: true, headers: true})
    .on("record", function (data) {
        data.ordinalValue = count;
        count = count + 1;
        csvOutputStream.write(data);
    })
    .on("end", function () {
        csvOutputStream.write(null);
    });

pietercolpaert pushed a commit to pietercolpaert/fast-csv that referenced this issue Jan 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants