[英]FasterCSV: columns into an array — rails
我试图获得更快的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
任何帮助表示赞赏。
感谢EmFi,在您的帮助下,我提出了解决方案。
这将获取一个远程url csv文件,并根据列将其加载到多维数组中。
require 'rio'
require 'fastercsv'
url = 'http://remoteurl.com/file.csv'
people = FasterCSV.parse(rio(url).read)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.