Created
June 8, 2016 16:12
-
-
Save hgwood/bb89a9fd80256c7f6f5ac71a091001bf to your computer and use it in GitHub Desktop.
Transforming CSV data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs") | |
// The 3 following modules need to be installed | |
const csvStream = require("csv-stream") | |
const csvWriteStream = require("csv-write-stream") | |
const through2Map = require("through2-map") | |
fs.createReadStream("in.csv") | |
.pipe(csvStream.createStream({ | |
// will use the first line as a header containing the keys for the resulting objects | |
delimiter: ";" // other options: https://www.npmjs.com/package/csv-stream | |
})) | |
.pipe(through2Map.obj(row => { | |
// This function is called for each row in the CSV file | |
// row is an object with a key for each column: row.column1, row.column2... | |
// This function should return the transformed object. | |
// e.g. | |
// return Object.assign(row, { | |
// column1: "value", | |
// column2: parseInt(row.column2) + 1 | |
// }) | |
})) | |
.pipe(csvWriteStream({ | |
separator: ";" // other options: https://www.npmjs.com/package/csv-write-stream | |
})) | |
.pipe(fs.createWriteStream("out.csv")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment