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

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a million. Yes it is simple indeed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.