Ruby on Rails从CSV移动到FasterCSV [英] Ruby on Rails Moving from CSV to FasterCSV

查看:121
本文介绍了Ruby on Rails从CSV移动到FasterCSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有以下代码使用标准csv库解析csv文件

  @ parsed_file = CSV :: Reader .parse(params [:dump] [:file])
@ parsed_file.each do | row |
#some code
end

我想将其移动到更快的csv增加速度。



感谢

解决方案

pre> CSV :: Reader.parse(File.open('file.csv')){| row | puts row}

CSV :: Reader.parse(some,content\\\
another,content){| row | puts row}

  FasterCSV.parse(File.open('file.csv')){| row | puts row} 

FasterCSV.parse(some,content\\\
another,content){| row | puts row}

等同。



  FasterCSV.read('filename')

使用文件名作为参数,从文件中读取和解析数据,但是您在传递参数

中的数据时转储文件内容

  @parsed_file = FasterCSV.parse(params [:dump] [:file])
@ parsed_file.each do | row |
put row
#并执行一些操作
end

工作正常。


I currently have the following code to parse a csv file using the standard csv library

@parsed_file=CSV::Reader.parse(params[:dump][:file])
@parsed_file.each  do |row|
#some code
end

I want to move this to faster csv for the increased speed. Does anyone know the equivalent of the above for FasterCSV?

Thanks

解决方案

CSV::Reader.parse(File.open('file.csv')){|row| puts row} 
or
CSV::Reader.parse("some, content\nanother, content"){|row| puts row} 

and

FasterCSV.parse(File.open('file.csv')){|row| puts row}
or
FasterCSV.parse("some, content\nanother, content"){|row| puts row}

are equivalent.

But

FasterCSV.read('filename') 

takes filename as parameter and reads and parse data from the file however you are dumping the file content as you are passing data in the parameter

@parsed_file = FasterCSV.parse(params[:dump][:file])
@parsed_file.each do |row| 
  puts row
  # and do some operations
end

should work fine.

这篇关于Ruby on Rails从CSV移动到FasterCSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆