简体   繁体   English

删除文件中的行 - Ruby

[英]remove rows in file - Ruby

What is a clever way to remove rows from a CSV file in ruby where a particular value exists in a particular row? 在特定行中存在特定值的ruby中从CSV文件中删除行的巧妙方法是什么?

Here's an example of a file: 这是一个文件的例子:

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

Ideally, I'd want a new file created with only 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

when given this: 给这个时:

300-2580
300-3080
300-2080

so I know i can do this with sort filename|uniq -d but I'm trying to learn Ruby (somewhat painfully). 所以我知道我可以用sort filename|uniq -d做到这一点,但我正在努力学习Ruby(有点痛苦)。

Thanks in advance, M 先谢谢,M

You can use this to get the unique lines in an array in a csv file 您可以使用它来获取csv文件中数组中的唯一行

File.readlines("file.csv").uniq
=> ["350 lbs., Outrigger Footprint, 61\" x 53\", Weight, 767 lbs., 300-2080\n", "350 lbs., Outrigger Footprint, 61\" x 53\", Weight, 817 lbs., 300-2580\n", "350 lbs., Outrigger Footprint, 69\" x 61\", Weight, 867 lbs., 300-3080\n"]

To write it to a new file, you can open a file in write mode, write this into the file: 要将其写入新文件,您可以在写入模式下打开文件,将其写入文件:

File.open("new_csv", "w+") { |file| file.puts File.readlines("csv").uniq }

For comparing values, you can use split function on ",", to access each column like this: 为了比较值,您可以使用“,”上的拆分功能来访问每列,如下所示:

rows = File.readlines("csv").map(&:chomp) # equivalent to File.readlines.map { |f| f.chomp }
mapped_columns = rows.map { |r| r.split(",").map(&:strip) }
=> [["350 lbs.", " Outrigger Footprint", " 61\" x 53\"", " Weight", " 767 lbs.", " 300-2080"], ["350 lbs.", " Outrigger Footprint", " 61\" x 53\"", " Weight", " 817 lbs.", " 300-2580"], .....]
mapped_columns[0][5]
=> "300-2080"

If you want more functionality, you are better off installing FasterCSV gem . 如果您想要更多功能,最好安装FasterCSV gem

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

Well, I don't think this example will get the answer you are looking for... but this would work... 好吧,我不认为这个例子会得到你想要的答案......但是这样可行......

tmp.txt => 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: File.readlines('tmp.txt').uniq将返回:

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. 因此,您也可以使用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. 谷歌ruby数组,我相信你可以学习如何根据与所需字符串的比较来选择条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM