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 
  1. read data
  2. transpose - ruby 1.9.3 should know #transpose
  3. 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

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -