csv.reader() vs csv.writer()
csv.reader() and csv.writer() 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.reader(): Returns a reader object that iterates over lines in a CSV file. Its syntax is csv.reader(csvfile, dialect="excel", **fmtparams), and it returns cSV reader iterator 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 Example of csv.reader(): import csv
with open("data.csv") as f:
for row in csv.reader(f):
print(row) # ["col1", "col2", ...]. Example of csv.writer(): with open("out.csv", "w", newline="") as f:
w = csv.writer(f)
w.writerow(["Name", "Age"])
w.writerow(["Alice", 30]). Read the full references for csv.reader() and csv.writer() to see parameters, edge cases, and example code side by side. Other CSV Module APIs worth comparing: csv.DictReader(), csv.DictWriter(), csv.writer.writerow(), csv.writer.writerows(), csv.Sniffer().sniff().