0

我正在尝试获得更快的csv设置,以便它不会解析每一行,而是将每一列放入一个多数组中。

CSV import file:
id, first name, last name, age
1, joe, smith, 11
2, jane, doe, 14

Save to array named people:
people[0][0] would equal id
people[2][1] would equal jane

这是我目前拥有的:

url = 'http://url.com/file.csv'
open(url) do |f|
  f.each_line do |line|
    FasterCSV.parse(line) do |row|
      row
    end
  end
end

任何帮助表示赞赏。

4

3 回答 3

3

您是否阅读过FasterCSV 文档?

如果你这样做了,你就会知道做你想做的最简单的方法是:

people = FasterCSV.read('http://url.com/file.csv')
于 2009-11-02T06:58:26.963 回答
0

感谢 EmFi,在您的帮助下,我想出了一个解决方案。

这需要一个远程 url csv 文件并将其加载到基于列的多维数组中。

require 'rio'
require 'fastercsv'

url = 'http://remoteurl.com/file.csv'
people = FasterCSV.parse(rio(url).read)
于 2009-11-02T19:22:33.900 回答
0

您可以在FasterCSV之上使用 CsvMapper 它是救生员

于 2009-11-03T11:23:16.963 回答