Last active
August 29, 2015 14:09
-
-
Save miguelmota/fa9001e7ecf42108d23a to your computer and use it in GitHub Desktop.
CSV in, JSON out
This file contains hidden or 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
var fs = require('fs'); | |
var csv = require('csv'); | |
var _ = require('lodash'); | |
var dir = './projection-coefficients/'; | |
var out = 'coeff.json'; | |
var obj = {}; | |
function done() { | |
var o = fs.createWriteStream(out, {encoding: 'utf8'}); | |
o.once('open', function(fd) { | |
console.log(JSON.stringify(obj)); | |
o.write(JSON.stringify(obj)+'\n'); | |
o.end(); | |
}); | |
} | |
fs.readdir(dir, function(err, files) { | |
var c = 0; | |
var fnames = files.reduce(function(a,f,i) { | |
if (/\.csv$/.test(f)) a.push(f); | |
return a; | |
}, []); | |
fnames.forEach(function(f) { | |
var input = fs.createReadStream(dir.concat(f)); | |
var key = f.replace(/\..*/gi,''); | |
input.on('data', function(d) { | |
csv.parse(d.toString(), function(err, output) { | |
var headers = output[0]; | |
headers.splice(0, 1, 'index'); | |
obj[key] = []; | |
output.slice(1).forEach(function(l) { | |
obj[key].push(_.zipObject(headers, l)); | |
}); | |
c += 1; | |
if (c === fnames.length) done(); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment