How to remove a row from a CSV with Ruby

前端 未结 2 1851
别那么骄傲
别那么骄傲 2021-01-13 09:59

Given the following CSV file, how would you remove all rows that contain the word \'true\' in the column \'foo\'?

Date,foo,bar
2014/10/31,true,derp
2014/10/         


        
2条回答
  •  生来不讨喜
    2021-01-13 10:24

    You might want to filter rows in a ruby manner:

    require 'csv' 
    csv = CSV.parse(File.read(@csvfile), {
      :col_sep => ",", 
      :headers => true
      }
    ).collect { |item| item[:foo] != 'true' }
    

    Hope it help.

提交回复
热议问题