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
Solution 1:
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, ...)
Solution 2:
You can read the file without saving the contents. Try:
withopen(filename, "r") as filehandle:
number_of_lines = len(filehandle.readlines())
Post a Comment for "How To Know Csv Line Count Before Loading In Python?"