Back to Python
2026-03-265 min read

Write CSV files with csv.DictWriter() (Python Programming)

Learn Write CSV files with csv.DictWriter() (Python Programming) step by step with clear examples and exercises.

Title: Writing CSV Files with csv.DictWriter() (Python Programming)


Why This Matters

In data analysis and manipulation, CSV (Comma Separated Values) files are a popular choice due to their simplicity and wide compatibility across various software applications. Python's built-in csv module allows you to read and write CSV files easily. In this lesson, we will focus on using the csv.DictWriter() class to write dictionaries as rows in CSV files, making it simpler to manage structured data.


Prerequisites

To follow this tutorial, you should have a basic understanding of Python programming and its syntax. Familiarity with working with dictionaries is essential for this lesson. If you are new to Python or need a refresher on dictionaries, you can check out our Python Dictionaries tutorial.


Core Concept

The csv.DictWriter() class is a convenient way to write dictionaries as rows in CSV files. It automatically handles the process of converting dictionary keys and values into strings, and correctly escaping any special characters that might appear in your data.

To create a DictWriter, you first need to open the CSV file using Python's built-in open() function with 'w' mode (for writing). Next, initialize the DictWriter object by passing the opened file and a list of fieldnames, which specify the keys in your dictionaries that will be written as columns in the CSV file.

Here is an example of creating a DictWriter:

import csv

data = [
{'Name': 'John', 'Age': 25, 'City': 'New York'},
{'Name': 'Sarah', 'Age': 30, 'City': 'Los Angeles'}
]

with open('output.csv', 'w', newline='') as csvfile:
fieldnames = ['Name', 'Age', 'City']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

Write the header (column names)

writer.writeheader()

Write the data

for row in data:

writer.writerow(row)


In this example, we define some sample data as a list of dictionaries and open a new CSV file called 'output.csv' with write permissions. We then initialize a `DictWriter` object named `writer`, specifying the fieldnames for our CSV columns. The `writeheader()` method writes the column names to the CSV file, and the `writerow()` method adds each row of data as a new line in the file.

---

Worked Example

Let's create a simple Python script that reads data from an external source (a list of dictionaries) and writes it to a CSV file using csv.DictWriter(). We will use the popular pandas library to load sample data from a CSV file, then write the data back to another CSV file using our custom script.

import csv
import pandas as pd

Load data from an external source (CSV file) using pandas

data = pd.read_csv('input.csv')

Prepare the output CSV file for writing

with open('output.csv', 'w', newline='') as csvfile:

fieldnames = list(data.columns) # Get fieldnames from dataframe columns

writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

Write the header (column names)

writer.writeheader()

Write the data

for index, row in data.iterrows():

writer.writerow({c: str(row[c]) for c in fieldnames})


In this example, we first use pandas to load data from an 'input.csv' file into a DataFrame. We then open the 'output.csv' file for writing and initialize a `DictWriter` object using the column names from our DataFrame. For each row in the DataFrame, we create a new dictionary with the same keys as our fieldnames, convert the values to strings, and write the row to the CSV file using `writerow()`.

---

Common Mistakes

  1. Not specifying fieldnames: When initializing the DictWriter, make sure to pass a list of fieldnames that match the keys in your dictionaries. If you don't specify fieldnames, the order of keys in your dictionaries will be used instead, which might not always produce the desired results.
  1. Not escaping special characters: The csv.DictWriter() class automatically handles escaping special characters, but if you encounter issues with data that contains problematic characters, consider using a function like csv.escape() to manually escape values before writing them to the CSV file.
  1. Writing lists or tuples instead of dictionaries: The DictWriter requires each row to be a dictionary, so make sure your data is properly formatted as such. If you're using list comprehensions to create rows, ensure that each element in the list is a dictionary.

Practice Questions

  1. Write a Python script that reads data from an external CSV file and writes it to another CSV file using csv.DictReader() and csv.DictWriter(). (Hint: Use pandas to load the data.)
  2. Given the following list of dictionaries, write a Python script that creates a new CSV file using csv.DictWriter().
data = [
{'Name': 'Alice', 'Age': 28, 'City': 'San Francisco'},
{'Name': 'Bob', 'Age': 35, 'City': 'Seattle'},
{'Name': 'Charlie', 'Age': 42, 'City': 'New York'}
]

FAQ

  1. What happens if I try to write a row with missing keys in my dictionaries?

If you attempt to write a row with missing keys, the DictWriter will raise a KeyError. To avoid this, make sure all rows in your data have the same keys as your fieldnames.

  1. Can I use csv.DictWriter() to write CSV files with different column orders for each row?

No, since the DictWriter relies on a fixed order of columns (defined by your fieldnames), it is not suitable for writing CSV files where the column order changes from one row to another. In such cases, consider using the regular csv.writer() instead.

  1. How can I handle cases where values in my dictionaries are empty strings or None?

If you encounter empty strings or None values in your data, you can use conditional statements to replace them with appropriate string representations before writing the row to the CSV file. For example:

if row['Age'] is None:
row['Age'] = ''
Write CSV files with csv.DictWriter() (Python Programming) | Python | XQA Learn