Skip to content

Instantly share code, notes, and snippets.

@prodoxx
Last active September 24, 2017 07:47
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 prodoxx/9a9c73151424cc74990acd9c10e338ca to your computer and use it in GitHub Desktop.
Save prodoxx/9a9c73151424cc74990acd9c10e338ca to your computer and use it in GitHub Desktop.
A script that converts a YAML skills file into a TSV file. Take two parameters from command line: the name of input and output file(optional). Prints the output in the command-line if output filename is not provided.
require 'yaml'
data_filename = ARGV[0]
output_filename = ARGV[1]
def yml_to_tsv(data)
headers = data[0].keys
header_line = ''
new_data = []
headers.map! { |header| header_line.concat(header, "\t") }
new_data.push(header_line).push("\n")
data.each do |set|
set.each_value { |val| new_data.push(val + "\t") }
new_data.push("\n")
end
new_data
end
yaml_data = YAML.safe_load(File.read(data_filename))
tsv_data = yml_to_tsv(yaml_data)
if output_filename
File.open(output_filename, 'w') { |file| file.puts tsv_data.join('') }
end
print tsv_data.join('') unless output_filename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment