简体   繁体   English

FasterCSV:列到数组中— rails

[英]FasterCSV: columns into an array — rails

I'm trying to get fastercsv setup so that rather than parsing each row, it will place each column into an multi array. 我试图获得更快的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

This is what I currently have: 这是我目前拥有的:

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

Any help is appreciated. 任何帮助表示赞赏。

Have you read the FasterCSV documentation? 您阅读过FasterCSV文档吗?

If you did, you would know that the easiest way to do what you want is: 如果这样做了,您将知道做您想要的事的最简单方法是:

people = FasterCSV.read('http://url.com/file.csv')

Thanks EmFi, with your help I was able to come up with a solution. 感谢EmFi,在您的帮助下,我提出了解决方案。

This takes a remote url csv file and loads it into a multi-dimensional array, based on columns. 这将获取一个远程url csv文件,并根据列将其加载到多维数组中。

require 'rio'
require 'fastercsv'

url = 'http://remoteurl.com/file.csv'
people = FasterCSV.parse(rio(url).read)

您可以在FasterCSV上使用CsvMapper ,这是一个救生器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM