Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This is a little node app that creates individual csv files for each tag in an exported Drip subscriber (people!) csv
const _ = require('lodash')
const fs = require('fs')
const csv = require('csv-parser')
const writer = require('fast-csv')
const csvFilePath = 'PATH_TO.csv'
const filesToBuild = {}
fs.createReadStream(csvFilePath)
.pipe(csv())
.on('data', function(row) {
const filterFor = 'sent - ' // only looking for tags with a specific string
row.tags = _.filter(row.tags.split(','), tag => tag.includes(filterFor))
row.tags.forEach(tag => {
const { tags, ...props } = row
//each tag will get its own csv output, so add it to the hash
filesToBuild[tag] = filesToBuild[tag] || []
filesToBuild[tag].push({ ...props })
})
})
.on('end', function() {
_.keys(filesToBuild).forEach(key => {
//go through all the filtered tags and write a csv file with the appropriate rows
writer.writeToPath(`./${key}.csv`, filesToBuild[key], { headers: true }).on('finish', function() {
console.log(`wrote ${key}.csv`)
})
})
})
{
"name": "parse_drip",
"version": "1.0.0",
"description": "",
"main": "drip_to_convertkit.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"csv-parser": "^2.1.0",
"fast-csv": "^2.4.1",
"lodash": "^4.17.11"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment