Good job! As you know, the first line in a CSV file may contain a header specifying the names of the fields (columns).
If we know that there is a header line in our CSV file, we can make good use of it:
with open('departments.csv') as csv_file:
csv_reader = csv.reader(csv_file)
line_count = 0
for row in csv_reader:
if line_count == 0:
print('Columns found:', ", ".join(row))
print('Department found:', ", ".join(row))
line_count += 1
We introduced a new variable named
line_count. This variable is increased each time we have processed a line. If the variable equals zero, which it always does on the first iteration through the loop, it means we're processing the first line – that is, the CSV header. You can see that the script's behavior is different for the first line, where we print "Columns found:".