Skip to content

Instantly share code, notes, and snippets.

@hgwood
Created June 8, 2016 16:12
Show Gist options
  • Save hgwood/bb89a9fd80256c7f6f5ac71a091001bf to your computer and use it in GitHub Desktop.
Save hgwood/bb89a9fd80256c7f6f5ac71a091001bf to your computer and use it in GitHub Desktop.
Transforming CSV data
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