0

is there any way to find out the number of rows and columns in csv file without for loop and pandas library

My code :

file=b''join(file).split(b'\n')
row_count=0
col_count=0
for line in file:
 row_count+=1
 line=re.split(b''',()''',line) #regex
 for columns in line:
   col_count+=1
print(row_count)
print(col_count)

is there any csv library are there to find the number of rows and columns.regex statement was there to handle delimeter.So no worry about it

  • `row_count = len(file)` and `col_count = file[0].count(',') + 1` – Mechanic Pig May 18 '22 at 18:27
  • Why do you want to avoid looping? You may get lucky and be able to rely on the line count and comma count as surrogates for rows and columns, but the CSV spec allows for rows and columns that don't fit those simple approximations. In the end, the only true way to count rows and columns is to **_parse_** the whole file with a good parser (the CSV module in Python is a good parser) and see what you have. – Zach Young May 18 '22 at 22:21
  • @MechanicPig-it doesnt work...i got an error "argument should be int or bytes-like obj not string – Prasanna Balaji May 19 '22 at 08:44
  • @ZachYoung--reason i avoid is processing time.Is there any way without looping to determine rows and columns – Prasanna Balaji May 19 '22 at 08:45

0 Answers0