Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know CSV line count before loading in python?

I am new to python and have a requirement to load dataframes from various CSV files. It turns out that there is a business logic depending on the number of rows in csv. Can i know this beforehand if i can know CSV total row numbers without writing read_csv?

like image 547
Avij Avatar asked Nov 16 '25 22:11

Avij


1 Answers

yes, you can:

lines = sum(1 for line in open('/path/to/file.csv'))

but be aware that Pandas will read the whole file again

if you are sure that the whole file will fit into memory we can do this:

with open('/path/to/file.csv') as f:
    data = f.readlines()
    lines = len(data)
    df = pd.read_csv(data, ...)
like image 89
MaxU - stop WAR against UA Avatar answered Nov 18 '25 12:11

MaxU - stop WAR against UA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!