1

I have a csv and I need to be able to print the total number of records how is this done? I have tried using sum statements and count but nothing seems to be working

user2807083
  • 2,962
  • 4
  • 29
  • 37
rohan
  • 23
  • 5

2 Answers2

1

Try this:

with open(adresse,"r") as f:
    reader = csv.reader(f,delimiter = ",")
    data = list(reader)
    row_count = len(data)
print(row_count)
Nothing
  • 502
  • 3
  • 11
0

Did you use pandas to import the csv file?

If so, here are some quick and easy options for obtaining the record count:

df = pandas.read_csv(filename)

len(df)
df.shape[0]
df.index

Otherwise, an alternative solution if you used csv.reader(filename.csv) is:
row_count = sum(1 for line in open(filename))

(this solution was originally suggested here)

adono
  • 18
  • 6