My program needs to read csv files which may have 1,2 or 3 columns, and it needs to modify its behaviour accordingly. Is there a simple way to check the number of columns withou
import csv
datafilename = 'testfile.csv'
d = '\t'
f = open(datafilename,'r')
reader = csv.reader(f,delimiter=d)
ncol = len(next(reader)) # Read first line and count columns
f.seek(0) # go back to beginning of file
for row in reader:
pass #do stuff