Welcome folks today in this blog post we will be counting number of lines or rows in csv file using csv module.
All the full source code of the application is given below.
Get Started
In order to get started you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 |
import csv file = open("sample.csv") reader = csv.reader(file) lines= len(list(reader)) print(lines) |
Now if you execute the python
script by typing the below command as shown below
python app.py
Using For Loop
1 2 3 4 5 6 |
num_rows = 0 for row in open("sample.csv"): num_rows += 1 print(num_rows) |