Skip to content

Instantly share code, notes, and snippets.

@callumj
Created June 20, 2013 03:27
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 callumj/5820090 to your computer and use it in GitHub Desktop.
Save callumj/5820090 to your computer and use it in GitHub Desktop.
Generates YAML files from a directory of CSVs using libraries that know what they are doing.
require 'csv'
require 'yaml'
dir ||= ENV["CSV_DIR"]
Dir["#{dir}/*.csv"].each do |file|
set = []
fields = []
index = 0
CSV.foreach(file) do |row|
if index == 0
fields = row.map { |f| f.chomp }
else
build = {}
fields.each_with_index do |field, i|
build[field] = row[i]
end
set << build
end
index += 1
end
File.write("#{file}.yml", set.to_yaml)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment