删除文件中的行 - Ruby

发布于 2024-12-01 01:45:12 字数 872 浏览 1 评论 0原文

在 ruby​​ 中从 CSV 文件中删除特定行中存在特定值的行的巧妙方法是什么?

这是一个文件的示例:

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

理想情况下,我希望仅使用以下内容创建一个新文件:

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

当给出以下内容时:

300-2580
300-3080
300-2080

所以我知道我可以使用 sort filename|uniq -d 来做到这一点,但我尝试学习 Ruby(有点痛苦)。

提前致谢, 中号

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., 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).

Thanks in advance,
M

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

那一片橙海, 2024-12-08 01:45:12

您可以使用它来获取 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"]

要将其写入新文件,您可以以写入模式打开文件,将其写入文件中:

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

要比较值,您可以使用 split 函数位于“,”上,以访问每一列,如下所示:

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"

如果您想要更多功能,最好选择安装FasterCSV gem

You can use this to get the unique lines in an array in a csv file

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.

小女人ら 2024-12-08 01:45:12

好吧,我认为这个例子不会得到您正在寻找的答案...但这会起作用...

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 将返回:

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

因此,您还可以轻松地使用 Array fxns 进行排序。 Google ruby​​ arrays,我相信您可以学习如何根据与所需字符串的比较来选择条目。

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.

蝶…霜飞 2024-12-08 01:45:12

您还可以创建一个不允许重复记录作为其条目的哈希。
例如,以下代码应该有所帮助:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文