Ignore rows with blank values while importing a CSV in rails application
This should work
CSV.open(import_file, skip_blanks: true).reject { |row| row.all?(&:nil?) }
EDIT
You requested for readlines, it calls open in the CSV source code in the end but this is it:
CSV.readlines(import_file, skip_blanks: true).reject { |row| row.all?(&:nil?) }
I feel open would perform better though I have not done any bench marking
CSV.open(import_file, skip_blanks: true, headers: true).reject { |row| row.to_hash.values.all?(&:nil?) }CSV.readlines(import_file, skip_blanks: true, headers: true).reject { |row| row.to_hash.values.all?(&:nil?) }
The above returns a collection of CSV::Row objects
This returns a CSV::Table object.
CSV.parse(import_file, headers: true, skip_blanks: true).delete_if { |row| row.to_hash.values.all?(&:blank?) }
The solution depends on if you read the CSV with or without headers, and if you want to keep working with a CSV::Table
object or if you are happy with an array of CSV::Row
Without headers you'll get rows that looks something like:
[#<CSV::Row nil nil nil]
With headers:
[#<CSV::Row "name":nil "abbreviation":nil "age":nil]
SOLUTION
Without headers and return array of CSV::Row
table.reject { |row| row.all?(&:nil?) }
Without headers and return CSV::Table
table.delete_if { |row| row.to_hash.values.all?(&:nil?) }
With headers and return array of CSV::Row
table.reject { |row| row.to_hash.values.all?(&:nil?) }
Without headers and return CSV::Table
table.delete_if { |row| row.to_hash.values.all?(&:nil?) }
FURTHER READING
Ruby Documentation for delete_if