Last active
September 24, 2017 07:47
-
-
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.
This file contains hidden or 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' | |
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