Skip to content

Instantly share code, notes, and snippets.

@macdiva
Forked from ashaw/wnyc_elections_parser.rb
Created October 6, 2010 20:02
Show Gist options
  • Save macdiva/613973 to your computer and use it in GitHub Desktop.
Save macdiva/613973 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'yajl/json_gem'
module AP
# a = AP::ParseData.new('/file.txt')
class ParseData
def initialize(file)
@file = file
@ap_scratch = []
@final_results = []
end
#a.parse
def parse
@ap_scratch = File.new(@file).readlines
@ap_scratch[7..68].each do |line|
h = {}
results = line.gsub(/[\000-\037]/,' ').gsub(/,/,'').scan(/(\w+)/)
h[:county] = results[0][0]
h[:precincts_total] = results[1][0]
h[:precincts_reporting] = results[2][0]
h[:cuomo] = results[3][0]
h[:paladino] = results[4][0]
h[:lazio] = results[5][0]
h[:schaeffer] = results[6][0]
@final_results << h
end
json_file = File.new('ny.json','w+')
json_file.write(@final_results.to_json)
json_file.close
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment