Change the format of csv file with Ruby -
i done research not find better solution change format of csv file. want convert row of csv file column, , convert column row.
for example:
original csv file:
a,b,c,d,e,f,g 1,2,3,4,5,6,7 7,6,5,6,4,2,1
i want convert above file
a,1,7 b,2,6 c,3,5 d,4,4 e,5,3 f,6,2 g,7,1
is there easier solutions?
so want transpose data
transposed = csv.read("path/to/file.csv").transpose csv.open("path/to/file.csv", "w") |csv| transposed.each |row| csv << row end end
- read data
- transpose - ruby 1.9.3 should know
#transpose
- write back
it's dead simple, provided csv isn't big read @ once.
if is, though, it's becomes quite horror. if that's case, refer answers this question.
Comments
Post a Comment