如何在 Ruby 中解析 CSV 文件中的列并将其存储为数组?

发布于 2024-09-09 02:44:58 字数 1018 浏览 1 评论 0原文

我有以下 CSV 文件:

Date,Av,Sec,128,440,1024,Mixed,,rule,sn,version

6/30/2010,3.40,343,352.0,1245.8,3471.1,650.7,Mbps,on,s-2.8.6-38,4.9.1-229,,vs. 342,-0.26%,-0.91%,1.51%,-0.97%
6/24/2010,3.40,342,352.9,1257.2,3419.5,657.1,Mbps,on,s-2.8.6-38,4.9.1-229,,vs. 341,0.23%,0.50%,-1.34%,0.67%
6/17/2010,3.40,341,352.1,1251.0,3466.1,652.7,Mbps,on,s-2.8.6-38,4.9.1-229,,vs. 340,7.77%,5.32%,9.04%,1.71%
6/14/2010,3.40,340,326.7,1187.8,3178.7,641.7,Mbps,on,s-2.8.6-38,4.9.1-229,,vs. 339,-0.88%,-0.34%,-0.95%,0.05%
6/11/2010,3.40,339,329.6,1191.9,3209.2,641.4,Mbps,on,s-2.8.6-38,4.9.1-229,,vs. 338,0.58%,0.51%,-1.83%,0.99%
6/11/2010,3.40,338,327.7,1185.8,3269.1,635.1,Mbps,on,s-2.8.6-38,4.9.1-229,,vs. 335,-0.40%,-0.44%,1.46%,-1.96%
6/11/2010,3.40,335,329.0,1191.0,3221.9,647.8,Mbps,on,s-2.8.6-38,4.9.1-229,,vs. 333,-6.83%,-4.70%,-7.04%,-0.32%
6/11/2010,3.40,333,353.1,1249.8,3465.8,649.9,Mbps,on,s-2.8.6-38,4.9.1-229,,vs. 332,2.53%,2.02%,1.71%,2.14%

我想解析第 4、5、6 和 7 列,并有四个数组,我可以在其中执行创建时间折线图等操作。

I have the following CSV file:

Date,Av,Sec,128,440,1024,Mixed,,rule,sn,version

6/30/2010,3.40,343,352.0,1245.8,3471.1,650.7,Mbps,on,s-2.8.6-38,4.9.1-229,,vs. 342,-0.26%,-0.91%,1.51%,-0.97%
6/24/2010,3.40,342,352.9,1257.2,3419.5,657.1,Mbps,on,s-2.8.6-38,4.9.1-229,,vs. 341,0.23%,0.50%,-1.34%,0.67%
6/17/2010,3.40,341,352.1,1251.0,3466.1,652.7,Mbps,on,s-2.8.6-38,4.9.1-229,,vs. 340,7.77%,5.32%,9.04%,1.71%
6/14/2010,3.40,340,326.7,1187.8,3178.7,641.7,Mbps,on,s-2.8.6-38,4.9.1-229,,vs. 339,-0.88%,-0.34%,-0.95%,0.05%
6/11/2010,3.40,339,329.6,1191.9,3209.2,641.4,Mbps,on,s-2.8.6-38,4.9.1-229,,vs. 338,0.58%,0.51%,-1.83%,0.99%
6/11/2010,3.40,338,327.7,1185.8,3269.1,635.1,Mbps,on,s-2.8.6-38,4.9.1-229,,vs. 335,-0.40%,-0.44%,1.46%,-1.96%
6/11/2010,3.40,335,329.0,1191.0,3221.9,647.8,Mbps,on,s-2.8.6-38,4.9.1-229,,vs. 333,-6.83%,-4.70%,-7.04%,-0.32%
6/11/2010,3.40,333,353.1,1249.8,3465.8,649.9,Mbps,on,s-2.8.6-38,4.9.1-229,,vs. 332,2.53%,2.02%,1.71%,2.14%

and I want to parse columns 4, 5, 6 and 7 and have four arrays, on which I can do operations like create a line graph against time, etc.

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

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

发布评论

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

评论(4

半仙 2024-09-16 02:44:58

您需要 Ruby 附带的 Ruby CSV 模块。例子:

require 'csv'
require 'pp'

file = File.open( 'bar.csv' )
CSV::Reader.create( file ).each do |row|
  pp row[4..7]
end

You need the Ruby CSV module which ships with Ruby. Example:

require 'csv'
require 'pp'

file = File.open( 'bar.csv' )
CSV::Reader.create( file ).each do |row|
  pp row[4..7]
end
旧人 2024-09-16 02:44:58

为什么要重新发明轮子!

使用插件 fastercsvcsv

why reinvent the wheel!

Use plugin fastercsv or csv

生生漫 2024-09-16 02:44:58

在使用 Ruby 解析 CSV 时,您会被多种选择所宠坏,因为标准库中包含一些选项,以及简单的自制方法或开源库。

您可以从“如何使用 Ruby 解析 CSV 数据”,这应该会为您指明深入挖掘的方向。

You are spoiled for choice in parsing CSVs with Ruby, as there are options included in the standard lib, as well as easy home-brewed methods or Open Source libs.

You can start with the examples on "How to parse CSV data with Ruby", and that should point you in the direction for digging deeper.

南风起 2024-09-16 02:44:58

您可以使用 smarter_csv Ruby gem 并使用 :key_mapping 来忽略不需要的输入列。

请参阅:

https://github.com/tilo/smarter_csv

you can use the smarter_csv Ruby gem and use a :key_mapping to ignore unwanted input columns.

See:

https://github.com/tilo/smarter_csv

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