[英]Ruby on Rails Moving from CSV to FasterCSV
我目前有以下代碼,可使用標准csv庫解析csv文件
@parsed_file=CSV::Reader.parse(params[:dump][:file])
@parsed_file.each do |row|
#some code
end
我想將此移動到更快的csv,以提高速度。 有人知道FasterCSV的上述功能嗎?
謝謝
CSV::Reader.parse(File.open('file.csv')){|row| puts row}
or
CSV::Reader.parse("some, content\nanother, content"){|row| puts row}
和
FasterCSV.parse(File.open('file.csv')){|row| puts row}
or
FasterCSV.parse("some, content\nanother, content"){|row| puts row}
是等效的。
但
FasterCSV.read('filename')
將filename作為參數並從文件中讀取和解析數據,但是當您在參數中傳遞數據時,您正在轉儲文件內容
@parsed_file = FasterCSV.parse(params[:dump][:file])
@parsed_file.each do |row|
puts row
# and do some operations
end
應該工作正常。
要使用文件路徑執行操作(如您所願):
FasterCSV.read(params[:dump][:file])
您可以查看FasterCSV文檔以了解其他方法(例如,在解析每一行時對其進行處理,或者從字符串而不是文件中讀取)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.