Skip to content

Instantly share code, notes, and snippets.

@toto
Created November 29, 2012 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toto/4169387 to your computer and use it in GitHub Desktop.
Save toto/4169387 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# encoding: utf-8
#
# Usage: ruby csv2json.rb filename.csv
#
require 'csv'
require 'pp'
require 'json'
filename = File.expand_path(ARGV.last)
if File.exist?(filename)
data = []
csv = CSV.new(File.read(filename).force_encoding("ISO-8859-1"), headers: true, col_sep: ";")
csv.each do |row|
data << {"vbb_id" => row["VBB-Nr"].to_s,
"hafas_id" => row["HAFAS-Nr"].to_s,
"name" => row["NAME"].to_s,
"lat" => row["lat"].to_f,
"lon" => row["lon"].to_f}
end
puts JSON.pretty_generate(data)
else
raise "No CSV file at #{filename}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment