Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

    <% form_for(:upcsv, :html => {:multipart => true}) do |f| %>     <table>         <tr>             <td><%= f.label("File:") %></td>             <td><%= f.file_field(:filename) %></td>         </tr>     </table>         <%= f.submit("Submit") %>     <% end %> 

Clicking Submit redirects me to another page (create.html.erb). The file was loaded fine, and I was able to read the contents just fine in this second page. I am trying to show the number of lines in the .csv file in this second page.

My controller (semi-pseudocode):

class UpcsvController < ApplicationController     def index     end      def create         file = params[:upcsv][:filename]         ...         #params[:upcsv][:file_length] = file.length # Show number of lines in the file         #params[:upcsv][:file_length] = file.size         ...     end end 

Both file.length and file.size returns '91' when my file only contains 7 lines. From the Rails documentation that I read, once the Submit button is clicked, Rails creates a temp file of the uploaded file, and the params[:upcsv][:filename] contains the contents of the temp/uploaded file and not the path to the file. And I don't know how to extract the number of lines in my original file. What is the correct way to get the number of lines in the file?

My create.html.erb:

<table>     <tr>         <td>File length:</td>         <td><%= params[:upcsv][:file_length] %></td>     </tr> </table> 

I'm really new at Rails (just started last week), so please bear with my stupid questions.

Thank you!

Update: apparently that number '91' is the number of individual characters (including carriage return) in my file. Each line in my file has 12 digits + 1 newline = 13. 91/13 = 7.

like image 277
Mathias Avatar asked Jan 11 '11 20:01

Mathias


People also ask

How do I count lines in a CSV file?

The wc command is used to count the number of words or lines (with the -l option) in a file. In a CSV file, each line is a data record.

How many lines can a CSV file have?

Cell Character Limits csv files have a limit of 32,767 characters per cell. Excel has a limit of 1,048,576 rows and 16,384 columns per sheet. CSV files can hold many more rows.

How do I count the number of rows and columns in a CSV file in Python?

To get the number of rows, and columns we can use len(df. axes[]) function in Python.


1 Answers

All of the solutions listed here actually load the entire file into memory in order to get the number of lines. If you're on a Unix-based system a much faster, easier and memory-efficient solution is:

`wc -l #{your_file_path}`.to_i 
like image 119
Jaco Pretorius Avatar answered Sep 30 '22 18:09

Jaco Pretorius