jeudi 16 janvier 2020

Rails CSV remove column that have empty header

I am receiving a csv file that has some blank headers but data exists in these columns. I want to remove the blank header and it's associated column in rails.

Sample csv

#report
,Code,Price,Orders,,Mark,
1,X91,4.55,4,xxx,F,23

What I'd like returned:

Code,Price,Orders,Mark
A91,4.55,4,F

This is what I have so far as there is also comments on the csv which i am ignoring.

CSV.open("output.csv", "w") do |output_csv|
          CSV.foreach("out.csv", encoding: "bom|utf-8", skip_lines: /^#/, headers: true).with_index(0) do |row, i|
            output_csv << row.headers if i == 0
            output_csv << row
          end
        end

Aucun commentaire:

Enregistrer un commentaire