Skip to content

Instantly share code, notes, and snippets.

@ashaw
Created October 2, 2010 20:18
Show Gist options
  • Save ashaw/607951 to your computer and use it in GitHub Desktop.
Save ashaw/607951 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
File.open(@file).each do |line|
@ap_scratch << line
end
@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