Created
September 2, 2020 21:23
-
-
Save pca2/d15f6c5d16952b2a58276b971c567aba to your computer and use it in GitHub Desktop.
Convert YML file to CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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