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

后端 未结 7 1229
野性不改
野性不改 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:14

    .length and .size are actually synonyms. to get the rowcount of the csv file you have to actually parse it. simply counting the newlines in the file won't work, because string fields in a csv can actually have linebreaks. a simple way to get the linecount would be:

    CSV.read(params[:upcsv][:filename]).length
    

提交回复
热议问题