Skip to content

Instantly share code, notes, and snippets.

@ctbarna
Created August 22, 2011 17:05
Show Gist options
  • Save ctbarna/1162917 to your computer and use it in GitHub Desktop.
Save ctbarna/1162917 to your computer and use it in GitHub Desktop.
Parse FDNY response time.
# Parse the FDNY average response time into JSON.
require 'rubygems'
require 'csv'
require 'json'
FILE = '../../Data/FDNY_monthly_response_time_001.TXT'
# Read the data
data = CSV.read(FILE, :col_sep => "\t",
:headers => true, :write_headers => false)
proc_data = []
# Format the data
data.each do |row|
# Months
if /[0-9]{6}/ =~ row[0]
row[0] = Date.strptime(row[0], fmt="%Y%m")
end
# Convert the count number.
row[3] = row[3].to_i
# Time
time = row[4].split(":")
time.each_index do |i|
time[i] = time[i].to_i
end
row[4] = time[0]*60+time[1]
# Zip everything up.
proc_data.push(Hash[*row.headers().zip(row.fields()).flatten])
end
File.open('fdny.json', 'w') {|f| f.write(JSON.generate(proc_data)) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment