user1703914
user1703914

Reputation:

Python counting number of rows n a csv

Hi I tried the following code to count the number of rows in a csv file :

import os
os.chdir('C:\\Users')
csv_file = open("count_rows.csv", "rb")
row_count = sum(1 for row in csv_file.read())
csv_file.close()
print (row_count)

The above code displays 18 as the result when the file has only 3 rows.

Any suggestions?

Thanks so much

Upvotes: 0

Views: 431

Answers (3)

Jan Vlcinsky
Jan Vlcinsky

Reputation: 44092

any text file (incl. csv) can have lines counted as follows:

>>> fname = "file.txt"
>>> with open(fname) as f:
...     for i, line in enumerate(f, 1):
...         pass
... print i

enumerate generates numbers for all lines, assigning it to i.

When the loop terminates, i holds the number of lines. Note, that the enumerate has the 1 argument used, to set up numbering from 1. (thanks falsetru)

Upvotes: 0

colcarroll
colcarroll

Reputation: 3682

csv_file should be an iterator -- you can just run sum(1 for row in csv_file).

Also, it is best practice to open the file with a context manager:

with open('count_rows.csv') as buff:
  row_count = sum(1 for _ in buff)

print(row_count)

Upvotes: 1

falsetru
falsetru

Reputation: 368954

The following line is not iterate lines, but each byte of the file content. (Iterating string object yields single-character strings)

row_count = sum(1 for row in csv_file.read())

To iterate lines, just iterate over the file object:

row_count = sum(1 for row in csv_file)

Here's a slighly modified version:

# Using `with` statement, you don't need to close manually.
# Use raw string literal: you can avoid escape
with open(r"C:\Users\count_rows.csv") as csv_file:  # `r`: use text mode
    row_count = sum(1 for row in csv_file)
    print(row_count)

Upvotes: 1

Related Questions