Skip to content

Instantly share code, notes, and snippets.

@samsee
Created February 14, 2019 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samsee/4e47c039ce651f6b72cc9f8596fb9ceb to your computer and use it in GitHub Desktop.
Save samsee/4e47c039ce651f6b72cc9f8596fb9ceb to your computer and use it in GitHub Desktop.
CSV to key, values concat
# Example
# READING FILE(CSV key,value format)
# k1,v1
# k1,v2
# k2,v3
# INTO
# k1 v1,v2
# k2 v3
# File open
f = File.open('concat.csv')
a = {}
f.each_line do |line|
s = line.split(',')
a[s[0]] = [] if a[s[0]].nil?
a[s[0]].push(s[1])
end
f.close
# File out
o = File.open('concat_result.out', 'w')
a.map { |k,v| o.puts k + "\t" + (v.each {|e| e.strip!}).join(","))}
o.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment