Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Row count in a csv file

Tags:

I am probably making a stupid mistake, but I can't find where it is. I want to count the number of lines in my csv file. I wrote this, and obviously isn't working: I have row_count = 0 while it should be 400. Cheers.

f = open(adresse,"r") reader = csv.reader(f,delimiter = ",") data = [l for l in reader] row_count = sum(1 for row in reader)  print row_count 
like image 388
Dirty_Fox Avatar asked Dec 16 '14 11:12

Dirty_Fox


People also ask

How do I count rows in a CSV file?

Using len() function Under this method, we need to read the CSV file using pandas library and then use the len() function with the imported CSV file, which will return an int value of a number of lines/rows present in the CSV file.

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

To get the number of rows, and columns we can use len(df.

How do I find the number of rows in a CSV file in Linux?

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. Counting the number of rows is an easy way to have the number of records in your CSV file.

Which function is used to count rows in a CSV file Rownumber?

Determines the ordinal number of the current row within a group of rows, counting from 1, based on the ORDER BY expression in the OVER clause.


1 Answers

with open(adresse,"r") as f:     reader = csv.reader(f,delimiter = ",")     data = list(reader)     row_count = len(data) 

You are trying to read the file twice, when the file pointer has already reached the end of file after saving the data list.

like image 80
jamylak Avatar answered Sep 20 '22 14:09

jamylak