Geeedas Geeedas - 3 days ago 7
Ruby Question

How to count empty range on csv files in one folder?

Suppose I have 7 CSV files in one folder two of them are empty on [2..-1] range, how do I count them and get answer 2?

Answer

This code iterates over the csv files in current folder, opens them, and checks that every cell in the 3rd, 4th, 5th... columns are empty in every line.

separator = ";"

empty_count = Dir["*.csv"].count do |csv|
  File.readlines(csv).all? do |line|
    line.split(separator,-1).drop(2).all? do |cell|
      cell.empty?
    end
  end
end
Comments