Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore rows with blank values while importing a CSV in rails application

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!

like image 486
Jackson Avatar asked Dec 18 '13 05:12

Jackson


4 Answers

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?) }
like image 176
Evgenii Avatar answered Oct 11 '22 19:10

Evgenii


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

like image 36
Guy C Avatar answered Nov 20 '22 19:11

Guy C


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

like image 12
bjhaid Avatar answered Nov 20 '22 21:11

bjhaid


This returns a CSV::Table object.

CSV.parse(import_file, headers: true, skip_blanks: true).delete_if { |row| row.to_hash.values.all?(&:blank?) }

like image 10
Keith Johnson Avatar answered Nov 20 '22 20:11

Keith Johnson