require 'yaml' require 'json' csv_data = [] File.open('tube_data_2006-07-26.csv','r').readlines[1..-1].each {|line| csv_data << line.split(',')} tube_data_hash = Hash.new {|key,value| key[value] = {}} csv_data.each do |line,direction,from_station,to_station,distance,unimpeded_time,peak_time,off_peak_time| tube_data_hash["#{from_station.gsub(/ \(.*\)/,'')}_#{line}"]["#{to_station.gsub(/ \(.*\)/,'')}_#{line}"] = {'line' => line, 'direction' => direction, 'distance' => distance.to_f, 'unimpeded_time' => unimpeded_time.to_f, 'peak_time' => peak_time.to_f, 'off_peak_time' => off_peak_time.to_f} end tube_data_hash.keys.each do |from_station| stations = tube_data_hash.find_all {|f_stat,t_stats| f_stat.split('_')[0] == from_station.split('_')[0] } stations.each do |f_stat,t_stats| tube_data_hash[from_station].merge! t_stats end end File.new('graph.yaml', 'w') << tube_data_hash.to_yaml File.new('graph.json', 'w') << tube_data_hash.to_json