Created
February 14, 2019 08:26
-
-
Save samsee/4e47c039ce651f6b72cc9f8596fb9ceb to your computer and use it in GitHub Desktop.
CSV to key, values concat
This file contains 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
# 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