I have a csv file named "employee_birthday.txt":
name,department,birthday month
John Smith,Accounting,November
Erica Meyers,IT,March
The followinng script manage to print the content properly:
import csv
with open('employee_birthday.txt', mode='r') as csv_file:
csv_reader = csv.DictReader(csv_file)
line_count = 0
for row in csv_reader:
if line_count == 0:
print(f'Column names are {", ".join(row)}')
line_count = 1
print(f'\t{row["name"]} works in the {row["department"]} department, and was born in {row["birthday month"]}.')
line_count = 1
print(f'Processed {line_count} lines.')
The output is as follows:
Column names are name, department, birthday month
John Smith works in the Accounting department, and was born in November.
Erica Meyers works in the IT department, and was born in March.
Processed 3 lines.
Actually this is from an online tutorial about csv. What puzzles me is the first iteration of the for loop. It seems the last two lines of the for loop got skipped magically and didn't print any thing during the first interation, even though there isn't a else
or continue
above the print (the 10th line). This doesn't make sense to me.
CodePudding user response:
If you just add a simple print('loop')
within your loop you'll see what's going on:
import csv
with open('test.csv', mode='r') as csv_file:
csv_reader = csv.DictReader(csv_file)
line_count = 0
for row in csv_reader:
print('loop') # prints once per loop
if line_count == 0:
print(f'Column names are {", ".join(row)}')
line_count = 1
print(f'\t{row["name"]} works in the {row["department"]} department, and was born in {row["birthday month"]}.')
line_count = 1
print(f'Processed {line_count} lines.')
The result is:
loop
Column names are name, department, birthday month
John Smith works in the Accounting department, and was born in November.
loop
Erica Meyers works in the IT department, and was born in March.
Processed 3 lines.
There's only 2 loop
s printed. Because in a DictReader
only the data values are looped through. The header row is already processed & headers are stored in the reader object.
When you do print(f'Column names are {", ".join(row)}')
the keys from the first data row dictionary are used to print that line since each row has the headers built-into it:
{'name': 'John Smith', 'department': 'Accounting', 'birthday month': 'November'}
You see the Processed 3 lines
because in your first iteration, line_count
is incremented twice.