import csv
# Writing to a CSV file
with open('data.csv', 'w', newline='') as csvfile:
fieldnames = ['name', 'age', 'city']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'name': 'John', 'age': 30, 'city': 'New York'})
writer.writerow({'name': 'Alice', 'age': 25, 'city': 'London'})
# Reading from a CSV file
with open('data.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print(row)Examples of code for writing to a csv file and reading from a csv file.


