csv.DictWriter()
csv.DictWriter() is a Python function in the CSV Module category. Returns a writer that maps dicts to CSV rows using fieldnames. The syntax is csv.DictWriter(csvfile, fieldnames, **fmtparams). Parameters: csvfile, fieldnames. Returns: DictWriter object. A typical example: with open("out.csv", "w", newline="") as f:
w = csv.DictWriter(f, fieldnames=["N","A"])
w.writeheader()
w.writerow({"N":"Alice", "A":30}). A close sibling is csv.reader(), which returns a reader object that iterates over lines in a CSV file. A close sibling is csv.writer(), which returns a writer object for writing rows to a CSV file. A close sibling is csv.DictReader(), which returns a reader that maps each row to a dict with field names as keys. A close sibling is csv.writer.writerow(), which writes a single row to the CSV file (row is a sequence of values). More about this category: CSV file reading and writing — reader, writer, DictReader, DictWriter. For a broader walkthrough, read the CSV Module guide.