Skip to content

Instantly share code, notes, and snippets.

@orison09
Last active September 22, 2018 16:31
Show Gist options
  • Save orison09/96b1962b5713273aa7436b4ac4551493 to your computer and use it in GitHub Desktop.
Save orison09/96b1962b5713273aa7436b4ac4551493 to your computer and use it in GitHub Desktop.
TSV and YML Converter
# Version 1.1 Basic structure. Added more.
require 'yaml'
require 'csv'
infile = ARGV[0]
yaml = File.read(infile)
data = YAML.safe_load(yaml)
key = data[0].keys
outfile = ARGV[1]
CSV.open(outfile, 'w', headers: false, col_sep: "\t") do |csv|
csv << key
data.each do |obj|
csv << key.map { |i| obj[i.to_s] }
end
end
# Version 1.1 Rubocop.
require 'yaml'
require 'csv'
infile = ARGV[0]
data = []
CSV.foreach(infile, headers: true, col_sep: "\t").each do |row|
hash = row.to_h
data << hash
end
outfile = ARGV[1]
File.write(outfile, data.to_yaml)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment