簡體   English   中英

FasterCSV:列到數組中— rails

[英]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

任何幫助表示贊賞。

您閱讀過FasterCSV文檔嗎?

如果這樣做了,您將知道做您想要的事的最簡單方法是:

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

感謝EmFi,在您的幫助下,我提出了解決方案。

這將獲取一個遠程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