Skip to content

Instantly share code, notes, and snippets.

@natanael-araujo
Last active October 7, 2017 23:57
Show Gist options
  • Save natanael-araujo/b8e5a45de04a33a53e02c40a89daaa8a to your computer and use it in GitHub Desktop.
Save natanael-araujo/b8e5a45de04a33a53e02c40a89daaa8a to your computer and use it in GitHub Desktop.
agrupa linhas por disciplina, considerando as colunas da primeira ocorrência e transformando as notas em colunas/desc_disciplina
#require 'pry'
require 'csv'
data = CSV.read('./resultado_bj.csv',
encoding: "UTF-8", headers: true,
header_converters: :symbol, converters: :all,
col_sep: '|', quote_char: '"'
)
data = data.map(&:to_hash)
result = {}
def transform(hash, new_hash=nil)
new_hash ||= hash
hash.tap do |h|
h[new_hash[:desc_disciplina]] = new_hash[:nota]
h.delete(:desc_disciplina)
end
end
data.each do |line|
mat = line[:matricula]
if result.key?(mat)
transform(result[mat], line)
else
result[mat] = transform(line)
end
end
columns = result.values.map(&:keys).flatten.uniq
CSV.open('./resultado_saci.csv', 'wb', quote_char: '"', col_sep: '|') do |csv|
csv << columns
result.values.each do |line|
csv << line.values_at(*columns)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment