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'})
11
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'})
1import csv
2with open("people.csv", 'r') as file:
3 csv_file = csv.DictReader(file)
4 for row in csv_file:
5 print(dict(row))
6