remove rows in file - Ruby

前端 未结 3 616
时光取名叫无心
时光取名叫无心 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 21:41

    Well, I don't think this example will get the answer you are looking for... but this would work...

    tmp.txt =>

    350 lbs., Outrigger Footprint, 61" x 53", Weight, 767 lbs., 300-2080
    350 lbs., Outrigger Footprint, 61" x 53", Weight, 817 lbs., 300-2580
    350 lbs., Outrigger Footprint, 61" x 53", Weight, 817 lbs., 300-2580
    350 lbs., Outrigger Footprint, 69" x 61", Weight, 867 lbs., 300-3080
    350 lbs., Outrigger Footprint, 69" x 61", Weight, 867 lbs., 300-3080
    

    File.readlines('tmp.txt').uniq will return this:

    350 lbs., Outrigger Footprint, 61" x 53", Weight, 767 lbs., 300-2080
    350 lbs., Outrigger Footprint, 61" x 53", Weight, 817 lbs., 300-2580
    350 lbs., Outrigger Footprint, 69" x 61", Weight, 867 lbs., 300-3080
    

    So, you could also easily sort with Array fxns. Google ruby arrays and I'm sure you can learn how to choose if you want an entry according to a comparison to a desired string.

提交回复
热议问题