Created
November 4, 2015 06:07
-
-
Save mixmix/be7757f80992c3beec6a to your computer and use it in GitHub Desktop.
for processing tsv form responses into a more useful csv
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
file_name = "./Tech for Non-tech (Responses) - Form responses 1.tsv" | |
file = File.new(file_name, "r") | |
lines = file.each_line.to_a | |
file.close | |
new_cols = [] | |
lines.each do |line| | |
next if line =~ /^Timestamp/ | |
selections = line.split(/\t/)[2]. | |
split(', '). | |
map {|el| el.chomp} | |
new_cols << selections | |
end | |
new_cols = new_cols. | |
flatten. | |
uniq | |
#p new_cols | |
#p new_cols.count | |
File.open( file_name.gsub(".tsv", ".csv"), "w") do |f| | |
f.puts (["Timestamp", "Name"] + new_cols).join(', ') | |
puts "here" | |
lines.each do |line| | |
next if line =~ /^Timestamp/ | |
cols = line.split(/\t/) | |
selections = cols[2].split(', '). | |
map {|el| el.chomp} | |
new_line = cols[0..1] | |
new_cols.each do |col| | |
if selections.include?(col) | |
new_line << 1 | |
else | |
new_line << 0 | |
end | |
end | |
f.puts new_line.join(', ') | |
end | |
end | |
puts "Done" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment