remove rows in file - Ruby

前端 未结 3 611
时光取名叫无心
时光取名叫无心 2021-02-10 20:59

What is a clever way to remove rows from a CSV file in ruby where a particular value exists in a particular row?

Here\'s an example of a file:

350 lbs.,          


        
3条回答
  •  情深已故
    2021-02-10 22:04

    You can also create a Hash which would NOT allow duplicate records as its entries . For example, the following code should help:

    require 'optparse'
    require 'csv'
    require 'pp'
    
    options = Hash.new
    
    OptionParser.new do |opts|
        opts.banner = "Usage: remove_extras.rb [options] file1 ..."
    
        options[:input_file] = ''
        opts.on('-i', '--input_file FILENAME', 'File to have extra rows removed') do |file|
            options[:input_file] = file
        end
    
    end.parse!
    if File.exists?(options[:input_file])
        p "Parsing: #{options[:input_file]}"
            UniqFile=Hash.new    
            File.open(options[:input_file]).each do |row|
            UniqFile.store(row,row.hash)                
    end
    puts "please enter the output filename: \n"
    aFile=File.open(gets.chomp, "a+")
    UniqFile.each do|key,value| 
    aFile.syswrite("#{key}")
    end  
    
    end
    

提交回复
热议问题