0

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

1
  • Why did you open the file with 'rb'? That is for binary files. You just want 'r'. Commented Jul 24, 2014 at 15:45

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

0

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)

1 Comment

If you use enumerate(f, 1), i will hold the number of lines. BTW, if the file is empty (0 byte), the last statement will raise NameError.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.