Skip to content

Instantly share code, notes, and snippets.

@pca2
Created September 2, 2020 21:23
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 pca2/d15f6c5d16952b2a58276b971c567aba to your computer and use it in GitHub Desktop.
Save pca2/d15f6c5d16952b2a58276b971c567aba to your computer and use it in GitHub Desktop.
Convert YML file to CSV
require 'yaml'
require 'csv'
yaml_file_path = ARGV[0] || nil
csv_file_path = ARGV[1] || nil
if yaml_file_path.nil? or csv_file_path.nil?
puts "USAGE: ruby ymltocsv.rb <input_path> <output_path>"
exit 1
end
parsed_yaml_file = YAML.load_file(yaml_file_path)
header = parsed_yaml_file.first.keys
puts "writing header"
CSV.open(csv_file_path, "wb") {|c| c << header}
puts "writing lines"
parsed_yaml_file.each do |line|
CSV.open(csv_file_path, "a") {|c| c << line.values}
end
puts "its over"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment