I have an app where I allow users to import CSV data.
Everything was working until users started importing data with blank rows in the CSV file. I am using the following to grab the rows out of the CSV file:
CSV.readlines(import_file, headers: true, skip_blanks: true)
I thought that if I added the option to skip_blanks that it would do that but it hasn't. Any ideas on how I can ignore the blank rows.
Thanks!
Here is how I managed remove blank lines from csv:
table = CSV.parse(csv_text, headers: true)
table.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
This should work
CSV.open(import_file, skip_blanks: true).reject { |row| row.all?(&:nil?) }
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?) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With