Find number of columns in csv file

You can use itertools.tee

itertools.tee(iterable[, n=2])
Return n independent iterators from a
single iterable.

eg.

reader1, reader2 = itertools.tee(csv.reader(f, delimiter=d))
columns = len(next(reader1))
del reader1
for row in reader2:
    ...

Note that it’s important to delete the reference to reader1 when you are finished with it – otherwise tee will have to store all the rows in memory in case you ever call next(reader1) again

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)