csv.writer() vs csv.DictReader()
csv.writer() and csv.DictReader() are both Python CSV Module APIs, and choosing between them is a common point of confusion. This page compares their syntax, parameters, return values, and typical use cases so you can pick the right one. csv.writer(): Returns a writer object for writing rows to a CSV file. Its syntax is csv.writer(csvfile, dialect="excel", **fmtparams), and it returns cSV writer object csv.DictReader(): Returns a reader that maps each row to a dict with field names as keys. Its syntax is csv.DictReader(csvfile, fieldnames=None, **fmtparams), and it returns dictReader iterator Example of csv.writer(): with open("out.csv", "w", newline="") as f:
w = csv.writer(f)
w.writerow(["Name", "Age"])
w.writerow(["Alice", 30]). Example of csv.DictReader(): with open("data.csv") as f:
for row in csv.DictReader(f):
print(row["Name"]) # Access by column name. Read the full references for csv.writer() and csv.DictReader() to see parameters, edge cases, and example code side by side. Other CSV Module APIs worth comparing: csv.reader(), csv.DictWriter(), csv.writer.writerow(), csv.writer.writerows(), csv.Sniffer().sniff().