Count the length (number of lines) of a CSV file?

后端 未结 7 1231
野性不改
野性不改 2020-12-31 02:46

I have a form (Rails) which allows me to load a .csv file using the file_field. In the view:

    <% form_for(:upcsv, :html => {:multipa         


        
7条回答
  •  失恋的感觉
    2020-12-31 03:01

    If your csv file doesn't fit to memory (can't use readlines), you can do:

    def self.line_count(f)
      i = 0
      CSV.foreach(f) {|_| i += 1}
      i
    end
    

    Unlike wc -l this counts actual record count, not number of lines. These can be different if there are new lines in field values.

提交回复
热议问题