Created
September 28, 2015 15:15
-
-
Save John-Lin/624dc81a8f065bf94009 to your computer and use it in GitHub Desktop.
For SOA Serialization homework
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
| # Check the output file | |
| if ARGV[1].nil? | |
| print 'Insert the name of your output file (yml file) ' | |
| outname = $stdin.gets.chomp | |
| else | |
| outname = ARGV[1] | |
| end | |
| # Build the array of Hashes | |
| require 'yaml' | |
| survey = [] | |
| lines = [] | |
| tsv_file = File.open(ARGV[0], 'r') | |
| tsv_file.each_line { |line| lines << line } | |
| tsv_file.close | |
| keys = lines[0].split("\t") | |
| keys.map!(&:chomp) | |
| lines.shift | |
| lines.each do |line| | |
| values = line.split("\t") | |
| record = Hash.new | |
| keys.each_index { |index| record[keys[index]] = values[index].chomp } | |
| survey.push(record) | |
| end | |
| # Serialize the data | |
| File.open(outname, 'w') do |file| | |
| file.puts survey.to_yaml | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment