2017-11-30 12 views
-1

行数を取得するためにlengthメソッドを試しましたが、行数を返すには少なくとも2分かかりました。長さメソッドは10 000まで高速で、100 000行を超えると遅すぎます。ファイルにも100k行以上含まれている2秒で行カウントを取得する方法はありますか?Rails CSVから行数を取得する方法

私は以下のコードを使用しました。

file = params[:file] 
f = File.open(file.path, 'r:iso-8859-1:utf-8') 
csv_file = CSV.parse(f.read, :headers => true, :converters => :all, :header_converters => lambda {|h| h.to_s.include?(' ') ? h.downcase.parameterize.underscore : h}) 
csv_file.length 
+0

https://stackoverflow.com/questions/2650517/count-the-number-of-lines-in-a-file-without-よりも高速で、解決策を見つけましたメモリ全体の読み込み – AbM

+0

https://stackoverflow.com/questions/4662438/count-the-length-number-of-lines-of-a-csv-file – seancdavis

+0

AbM&seancdavis - 私はすでに試してみました上記のとおり、改善はありません。 – anandh

答えて

0

は最終的に私はCSV.read(file.path).lengthCSV.parse(f.read).length

関連する問題