删除文件中的行 - Ruby [英] remove rows in file - Ruby

查看:195
本文介绍了删除文件中的行 - Ruby的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是一个文件的示例:

这是一个聪明的方法来从一个特定值存在于特定行的ruby中的CSV文件中删除行? / p>

  350磅,支腿脚印,61x 53,重量,767磅,300-2080 
350磅,脚趾脚印,61x 53,重量,817磅,300-2580
350磅,支腿脚印,61x 53,重量,817磅,300-2580
350磅,舷外脚印,69x 61,重量,867磅,300-3080
350磅,舷外脚印,69x 61,重量,867磅,300-3080

理想情况下,我只想创建一个新文件:

  350磅,支腿脚印,61x 53,重量,767磅,300-2080 
350磅,支腿脚印,61 x 53,重量,817磅,300-2580
350磅,舷外脚印,69x 61,重量,867磅,300-3080

给予这个:

  300-2580 
300-3080
300- 2080

所以我知道我可以用 sort filename | uniq -d 但我正在尝试学习Ruby(有点痛苦)。

提前感谢
M

解决方案

你可以使用它来获取csv文件中的数组中的唯一行。

  File.readlines(file.csv)。 uniq 
=> [350磅,支腿脚印,61,53,重量,767磅,300-2080,350磅,舷外脚印,61 \x 53 ,重量,817磅,300-2580 \,350磅,支腿脚印,69 \x 61 \,重量,867磅,300-3080 \

要将其写入新文件,可以在写入模式下打开文件,将其写入文件: / p>

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

为了比较值,您可以使用拆分功能,,以访问每一列,如下所示:

  rows = File.readlines(csv)。map(&:chomp)#等效于File.readlines.map {| f | f.chomp} 
mapped_columns = rows.map {| r | r.split(,)。map(&:strip)}
=> [[350磅,舷外脚印,61,53,重量,767磅,300-2080),[350磅舷外脚印,61 \x 53 \,重量,817磅,300-2580],.....]
mapped_columns [0] [5]
=> 300-2080

如果你想要更多的功能,你最好安装 FasterCSV宝石


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

解决方案

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.

这篇关于删除文件中的行 - Ruby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆