Instantly share code, notes, and snippets.

What would you like to do?
Quick script to parse NYC subway turnstile data
# As the following indicates, this script replies on the csv and async npm modules.
# It reads all the files placed inside a "csvs" directory, in the same directory as this file.
csv = require "csv"
fs = require "fs"
async = require "async"
outputArray = ['"ca","unit","scp","dt","desc","entries", "exits"']
files = fs.readdirSync "./csvs"
if fs.exists "./output.csv" then fs.delete "./output.csv"
str = fs.createWriteStream "./output.csv", {encoding:"UTF8"}
str.write outputArray.join(",")
async.eachSeries files, (f,cb) ->
console.log "Processing #{f}..."
toAdd = []
.on "record", (r) ->
for i in [1..8]
if !r["DATE#{i}"] then break
dateSplit = r["DATE#{i}"].split("-")
dt = "2013/#{dateSplit[0]}/#{dateSplit[1]} " + r["TIME#{i}"]
toAdd.push '"' + [r["C/A"], r["UNIT"], r["SCP"],dt,r["DESC#{i}"], parseInt(r["ENTRIES#{i}"]),parseInt(r["EXITS#{i}"])].join('","') + '"'
.on "end", () ->
str.write("\n" + toAdd.join("\n"))
, () ->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment