Skip to content

Instantly share code, notes, and snippets.

@prodoxx
Last active September 24, 2017 07:47
Show Gist options
  • Save prodoxx/2a3d85ef22a9727c75be6f6be0f352a9 to your computer and use it in GitHub Desktop.
Save prodoxx/2a3d85ef22a9727c75be6f6be0f352a9 to your computer and use it in GitHub Desktop.
A script that converts a TSV file into a YAML 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]
new_data = []
header = ''
def create_record(headers, line)
values = line.split("\t").map!(&:chomp)
headers = headers.split("\t").map!(&:chomp)
new_record = {}
headers.size.times { |index| new_record[headers[index]] = values[index] }
new_record
end
tsv_file = File.open(data_filename, 'r')
tsv_file.each_line.with_index do |line, index|
header = line if index.zero?
new_data.push(create_record(header, line)) unless index.zero?
end
tsv_file.close
if output_filename
File.open(output_filename, 'w') { |file| file.puts new_data.to_yaml }
end
print new_data.to_yaml unless output_filename
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment