0

I have almost 1000+ CSV files where I need to shuffle several columns and recreate each CSV file with the shuffled columns. As an example, initial CSV file has "H1", "H2" and "H3" columns. The new CSV file will have columns "H1", "H3" and "H2".

Using FasterCSV in Ruby how would I do it? I have tried FasterCSV::Table with column_name access but no luck.

Can some kind soul help?

Regards

K

pguardiario
  • 53,827
  • 19
  • 119
  • 159
kishore
  • 541
  • 1
  • 6
  • 18

1 Answers1

1

It's not too complicated:

CSV.open('new.csv', 'w') do |new_csv|
    CSV.foreach('old.csv') do |row|
        row[1], row[2] = row[2], row[1]
        new_csv << row
    end
end

I call it CSV instead of FasterCSV because that's the 1.9 way

pguardiario
  • 53,827
  • 19
  • 119
  • 159