rohan
rohan

Reputation: 23

How do I print the total number of records from a csv file in python?

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

Upvotes: 1

Views: 1922

Answers (2)

adono
adono

Reputation: 18

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)

Upvotes: 0

Nothing
Nothing

Reputation: 512

Try this:

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

Upvotes: 1

Related Questions