Dirty_Fox Dirty_Fox - 3 months ago 21
Python Question

Row count in a csv file

I am probably making a stupid mistake but I can t find where it is. I wan to count the number of line in my csv file. I wrote this, and obviously aint working : I hav 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

Answer
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.