Skip to content

Instantly share code, notes, and snippets.

@ysksn
Last active July 11, 2016 01:16
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 ysksn/7ba150c0e705b284620aefe8ccda90c9 to your computer and use it in GitHub Desktop.
Save ysksn/7ba150c0e705b284620aefe8ccda90c9 to your computer and use it in GitHub Desktop.
require 'csv'
csv_file = File.new('/Users/marketing-imac1/Desktop/tmp.csv')
json_file = File.open('/Users/marketing-imac1/Desktop/areas.json', 'w')
csv = CSV.open(csv_file, 'r').to_a
str = ''
last_index = csv.length - 1
block = lambda do |row, i|
closer = i == last_index ? "]" : "],"
str += ' [' + [('"' + row[0]) + '"', row[1], row[2], row[3]].join(', ') + closer + "\n"
end
csv.each_with_index(&block)
str = "var areas =\n" \
str \
'];'
File.open(json_file, 'w') { |file| file.puts str }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment