1import csv
2
3with open('employee_birthday.txt', mode='r') as csv_file:
4 csv_reader = csv.DictReader(csv_file)
5 line_count = 0
6 for row in csv_reader:
7 if line_count == 0:
8 print(f'Column names are {", ".join(row)}')
9 line_count += 1
10 print(f'\t{row["name"]} works in the {row["department"]} department, and was born in {row["birthday month"]}.')
11 line_count += 1
12 print(f'Processed {line_count} lines.')
13
1import csv
2
3with open('names.csv', 'w') as csvfile:
4 fieldnames = ['first_name', 'last_name']
5 writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
6
7 writer.writeheader()
8 writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'})
9 writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'})
10 writer.writerow({'first_name': 'Wonderful', 'last_name': 'Spam'})