How to Write CSV Files in Python (from list, dict)

How to Write CSV Files in Python (from list, dict) Cover Image

This guide will teach you how to write CSV files in Python, including from Python lists and dictionaries. The Python csv library gives you significant flexibility in writing CSV files. For example, you can write Python lists to CSV, including writing headers and using custom delimiters. Likewise, you can write Python dictionaries to CSV and handle non-ASCII characters.

By the end of this guide, you’ll have learned the following:

Table of Contents

Quick Answer: How to Write CSV Files from a List in Python

If you’re in a hurry, you can use the following code to write a CSV file from a Python list:

# Quick Answer: Write a Python List to a CSV File import csv header = ['Name', 'Age', 'Site', 'Location'] values = [ ['Nik', 34, 'datagy', 'Toronto'], ['Kate', 33, 'google', 'Paris'], ['Evan', 32, 'bing', 'New York City'], ['Kyra', 35, 'yahoo', 'Atlanta'] ] with open('write_to_csv.csv', 'w') as file: writer = csv.writer(file) writer.writerow(header) writer.writerows(values)
How can you write a Python list to a CSV file?

To write a Python list to a CSV file, you can use the csv library and the csv.writer() class. By passing a file into the class, you create an object that allows you to write single rows and multiple rows from a Python list.

Throughout this tutorial, we’ll explore how this method works, allowing you to customize the behavior. Use the table of contents above to skip to a specific section.

How to Write a Python List to a CSV File

In this section, you’ll learn how to write a Python list to a CSV file. Python comes with a library, csv , as part of its standard library. As the name implies, this library is used to handle CSV files, both in terms of reading and writing them.

One of the helpful classes that allow you to write CSV files from Python lists is the csv.writer() class. Let’s dive into how the class works and how you can use it to write CSV files from Python lists.

Understanding the csv.writer() Class in Python

The csv.writer() class can be used to insert data into a CSV file using Python. By passing a file object into the class, a new writer object is returned. This object converts a user’s input data into a delimited string. This string, by default, is comma-separated.

The class provides a number of helpful methods:

Let’s see how we can instantiate a csv.writer() class by passing in an opened file:

# Creating a csv.writer() Class Object import csv with open('filename.csv', 'w') as file: writer = csv.writer(file)

Let’s break down what is happening in the code block above:

  1. We import the csv module
  2. We then use a context manager to open a file in write mode, 'w' . By doing this, if the file doesn’t exist, Python will create it for you. We use the alias file to use the file.
  3. We then create a writer object by passing this new file into csv.writer()

Note here that we’re using a context manager. While this is not necessary, it allows you to run your code in a safer manner. This is because the context manager will close the file once it’s done being used. This allows your code to be more memory safe.

Now, let’s dive into how we can write a single row to a CSV file from a Python list.

How to Write a Single Row to a CSV File from a Python List

In order to write a single row to a CSV file, we can use the .writerow() method associated with a csvwriter() object. If we pass in a Python list of lists into the method, Python will write a single list at a time. Let’s see what this looks like:

# How to Write a Single Row to a CSV File import csv nik = ['Nik', 34, 'datagy.io', 'Toronto'], kate = ['Kate', 33, 'google', 'Paris'], with open('listtocsv.csv', 'w') as file: writer = csv.writer(file) writer.writerow(nik) writer.writerow(kate)

To write a single row at a time to a CSV in Python, you can follow the steps below:

  1. Import the csv module
  2. Create a single or multiple list variables
  3. Open a CSV file in write mode
  4. Instantiate a csv.writer() object by passing in the file as its argument
  5. Use the .writerow() method and pass in a single list. This will write the list to a single row in a CSV file.

This generates the following CSV file:

Nik,34,datagy,Toronto Kate,33,google,Paris

This process can be time consuming if you want to write multiple lines at a single time. Because of this, Python provides the .writerows() method. Let’s explore that in the following section.

How to Write Multiple Rows to a CSV File from a Python List

In order to write multiple rows to a CSV from a Python list of lists, you can use the .writelines() method found in the csv.writer() class.

Similar to our example above, we can create a CSV writer object. However, rather than writing a single line at a time, we can pass in multiple lines at once. Let’s see what this looks like in Python:

# How to Write Multiple Lines to a CSV File From a Python List of Lists import csv values = [ ['Nik', 34, 'datagy.io', 'Toronto'], ['Kate', 33, 'google', 'Paris'], ['Evan', 32, 'bing', 'New York City'], ['Kyra', 35, 'yahoo', 'Atlanta'] ] with open('listtocsv.csv', 'w') as file: writer = csv.writer(file) writer.writerows(values)

To write a multiple rows to a CSV in Python from a list of lists, you can follow the steps below:

  1. Import the csv module
  2. Create a list of lists that contains each row as a separate list
  3. Open a CSV file in write mode
  4. Instantiate a csv.writer() object by passing in the file as its argument
  5. Use the .writerows() method and pass in the list of lists. This will write multiple rows at a single time in a CSV file.

The code block above returns the following CSV file:

Nik,34,datagy.io,Toronto Kate,33,google,Paris Evan,32,bing,New York City Kyra,35,yahoo,Atlanta

We can see how much simpler this made our code! Now, let’s explore how to add a header to our CSV file from a Python list of lists.

How to Add a Header to a CSV File from a Python List

Python makes it simple to add a header row to our CSV file. This can be done by combining the two methods you have learned so far. In many cases, it doesn’t make sense to store your header row as a list in your data’s list of lists. Instead, the header is often maintained as a separate list or tuple.

Let’s see how we can add a header to a CSV file from a Python list:

# Including a Header in the CSV File import csv headers = ['Name', 'Age', 'Website', 'Location'] values = [ ['Nik', 34, 'datagy.io', 'Toronto'], ['Kate', 33, 'google', 'Paris'], ['Evan', 32, 'bing', 'New York City'], ['Kyra', 35, 'yahoo', 'Atlanta'] ] with open('listtocsv.csv', 'w') as file: writer = csv.writer(file) writer.writerow(headers) writer.writerows(values)

In the code block above, we:

  1. First wrote the header row using the .writerow() method.
  2. Then, we wrote used the .writerows() method to write the remaining rows of data from our list of lists.

This returns the following CSV file, which includes the header row:

Name,Age,Website,Location Nik,34,datagy.io,Toronto Kate,33,google,Paris Evan,32,bing,New York City Kyra,35,yahoo,Atlanta

In the following section, you’ll learn how to append a list to a CSV file in Python.

How to Append a List to a CSV File in Python

With each of the previous sections of the tutorial, the file that you wrote to was overwritten. This is something important to be aware of – even if the file exists, all data will be overwritten.

If we want to append a list to a CSV file in Python, we can simply modify the mode in which Python opens the file. So far, we have used a write mode of 'w' . By modifying the mode to append, using 'a' , we can append to our CSV file, rather than overwriting it.

Let’s see what this looks like in Python:

# Appending a List to a CSV File in Python import csv headers = ['Name', 'Age', 'Website', 'Location'] values = [ ['Nik', 34, 'datagy.io', 'Toronto'], ['Kate', 33, 'google', 'Paris'], ['Evan', 32, 'bing', 'New York City'], ['Kyra', 35, 'yahoo', 'Atlanta'] ] with open('listtocsv.csv', 'a') as file: writer = csv.writer(file) writer.writerows(values)

When we use our previously created CSV file, this returns the following CSV file:

Name,Age,Website,Location Nik,34,datagy.io,Toronto Kate,33,google,Paris Evan,32,bing,New York City Kyra,35,yahoo,Atlanta Nik,34,datagy.io,Toronto Kate,33,google,Paris Evan,32,bing,New York City Kyra,35,yahoo,Atlanta

In the following sections, you’ll learn how to write a Python dictionary to a CSV file.

How to Write a Python Dictionary to a CSV File with DictWriter

Similar to the csv.writer() object, the csv module provides a class to write dictionaries to CSV files. This class is called the csv.DictWriter() class and provides similar methods for writing data to CSV files.

How can you write a Python dictionary to a CSV file?

To write a Python dictionary to a CSV file, you can use the csv library and the csv.DictWriter() class. By passing a file into the class, you create an object that allows you to write single rows and multiple rows from a Python dictionary or list of dictionaries.

What’s different between this approach and the csv.writer() class is that the class assumes that we provide the field names of the CSV file. Let’s take a look at a basic implementation of the class:

# Creating a DictWriter Object in Python import csv headers = ['Name', 'Age', 'Website', 'Location'] with open('dicttocsv.csv', 'w') as file: writer = csv.DictWriter(file, fieldnames=headers)

It’s important to note that the instantiation will fail if we do not provide the fieldnames= argument. Python will raise a TypeError and instruct you to pass in a fieldnames argument.

Let’s dive into how we can use the csv.DictWriter() class to write single rows and multiple rows to a CSV file.

How to Write a Single Row to a CSV File from a Python Dictionary

In order to write a single dictionary to a CSV file in Python, you can use the csv.DictWriter() class. Unlike the csv.writer() class that we used to write lists to CSV files, the csv.DictWriter() class requires that we pass in the field names.

Why is that? Because Python dictionaries haven’t always been ordered, the order in which fields appear may differ. Similarly, Python dictionaries don’t let you access items by their position (unlike lists). Because of this, we need to find a way to tell Python the order in which to insert values.

Let’s take a look at how we can write a dictionary to a CSV file using the .writerow() method:

# Writing Single Dictionaries to a CSV File in Python import csv headers = ['Name', 'Age', 'Website'] nik = kate = with open('dicttocsv.csv', 'w') as file: writer = csv.DictWriter(file, fieldnames=headers) writer.writerow(nik) writer.writerow(kate)

In order to write a dictionary to a CSV file in Python, follow the steps below:

  1. Import the csv module
  2. Declare a list of strings that contain the header row
  3. Create a dictionary containing the headers as keys. The order in the dictionary doesn’t matter.
  4. Create a CSV file and pass it into the csv.DictWriter() class to instantiate a writer object. Pass in the headers into the fieldnames= parameter.
  5. Finally, we used the .writerow() method to write a dictionary as a CSV row. We can repeat this multiple times with individual dicationaries.

This returns the following CSV file:

Nik,34,datagy.io Kate,33,google

In the following section, you’ll learn how to write multiple dictionaries to a CSV file directly.

How to Write Multiple Rows to a CSV File from a Python Dictionary

In order to write multiple dictionaries to a CSV file with Python, you can use the .writerows() method of the csv.DictWriter() class. This allows you to pass in a list of Python dictionaries.

Being able to do this can be incredibly helpful when working with JSON data. Python provides a number of ways to convert JSON files to CSV.

Let’s take a look at how we can write a list of dictionaries to a CSV file:

# Write a List of Dictionaries to a CSV File in Python import csv headers = ['Name', 'Age', 'Website'] values = [ , , , ] with open('dicttocsv.csv', 'w') as file: writer = csv.DictWriter(file, fieldnames=headers) writer.writerows(values)

In this example, we used the .writerows() method to write a list of dictionaries to a CSV file. This allows you to add as much data as you want, without needing to call the .writerow() method for each dictionary.

This returns the following CSV file:

Nik,34,datagy.io Kate,33,google Evan,32,bing Kyra,35,yahoo

In the following section, you’ll learn how to add a header row to a CSV file from a list of dictionaries.

How to Add a Header to a CSV File from a Python Dictionary

It’s very easy to add a header row to a CSV file that’s created using Python dictionaries. Because we already pass in the field names when we create our writer object, we can simply use this information to create a header.

In fact, the csv.DictWriter() class comes with a method for just this purpose: .writeheader() . Let’s take a look at how we can write a header row for our CSV file:

# Write a Header to a CSV File import csv headers = ['Name', 'Age', 'Website'] values = [ , , , ] with open('dicttocsv.csv', 'w') as file: writer = csv.DictWriter(file, fieldnames=headers) writer.writeheader() writer.writerows(values)

The code block above is unchanged from the previous example except for the addition of the second last row. By calling the .writeheader() method, Python adds a header row. It uses the values passed into the fieldnames= parameter to create our header.

This returns the following CSV file:

Name,Age,Website, Nik,34,datagy.io Kate,33,google Evan,32,bing Kyra,35,yahoo

We can see that Python was able to successfully create a CSV file from our list of dictionaries, including inserting a header row.

How to Append a Dictionary to a CSV File in Python

With each of the previous sections of the tutorial, the file that you wrote to was overwritten. This is something important to be aware of – even if the file exists, all data will be overwritten.

If we want to append a list to a CSV file in Python, we can simply modify the mode in which Python opens the file. So far, we have used a write mode of 'w' . By modifying the mode to append, using 'a' , we can append to our CSV file, rather than overwriting it.

Let’s see what this looks like in Python:

# Appending a List to a CSV File in Python import csv headers = ['Name', 'Age', 'Website', 'Location'] values = [ , , , ] with open('dicttocsv.csv', 'a') as file: writer = csv.DictWriter(file, fieldnames=headers) writer.writerows(values)

When we use our previously created CSV file, this returns the following CSV file:

Name,Age,Website,Location Nik,34,datagy.io,Toronto Kate,33,google,Paris Evan,32,bing,New York City Kyra,35,yahoo,Atlanta Nik,34,datagy.io,Toronto Kate,33,google,Paris Evan,32,bing,New York City Kyra,35,yahoo,Atlanta

In the following sections of this tutorial, you’ll learn how to customize the resulting CSV files using special attributes, such as custom delimeters and quoted characters.

Additional Options in the CSV DictWriter Class

The csv.DictWriter() class provides a number of different options to customize how dictionaries are written to CSV files. In particular, the class provides two additional parameters:

How to Use Custom Delimiters when Writing to CSV in Python

We can customize the delimiter that is used to split columns in a CSV file. The delimeter= parameter is available for both the csv.writer() class and the csv.DictWriter() class. We can pass in a string that represents the delimiter we want to use.

Let’s see how we can customize the delimiter when using Python to write a list or a dictionary to a CSV file:

# Customizing the Delimeter In Python CSV Files import csv headers = ['Name', 'Age', 'Website', 'Location'] values = [ , , ] with open('dicttocsv.csv', 'w') as file: writer = csv.DictWriter(file, fieldnames=headers, delimiter="|") writer.writeheader() writer.writerows(values)

In the code above, we passed a pipe operator into the delimiter= argument. Keep in mind that this also works for the csv.writer() class. This returns the following CSV.

Name|Age|Website|Location Nik|34|datagy.io| Kate|33|google|

In the following section, you’ll learn how to create and use dialects that allow you to easily customize the way in which CSV files are written.

How to Use Dialects when Writing to CSV in Python

Dialects in the CSV module allow you to set customization criteria to easily write and read files in the same style. While we have only covered custom delimiters so far, the CSV library provides significant flexibility to customize CSV files. Because of this, dialects allow you to set preset styles that can be used to read and write CSV files in Python.

Let’s see how we can create a dialect and use it when writing a CSV file:

# Registering and Using a Dialect import csv csv.register_dialect('custom_dialect', delimiter='|') headers = ['Name', 'Age', 'Website', 'Location'] values = [ ['Nik', 34, 'datagy.io', 'Toronto'], ['Kate', 33, 'google', 'Paris'], ] with open('listtocsv.csv', 'w') as file: writer = csv.writer(file, dialect='custom_dialect') writer.writerow(headers) writer.writerows(values)

In the example above we created a dialect using the csv.register_dialect() function. The function allows you pass in a name using a string. Then, it can subclass either existing dialects or add custom formatting as we have done.

This allows you to easily re-use the dialect when you are writing other files or even reading CSV files. Because the function isn’t tied to a particular writer class, it works with both the csv.writer() class and the csv.DictWriter() class.

How to Deal with Quotes when Writing to CSV in Python

By default, Python will wrap individual entries in double quotes in order to ensure that items are properly delimited. Imagine the case where you list items have commas in them, Python needs a way to ensure that items are delimited appropriately.

Similarly, when we write items with quotes in it themselves, the csv module will escape quotes by adding double quotes. Let’s see what this looks like:

# Seeing What Happens with Quotes import csv headers = ['Name', 'Age', 'Quote'] values = [ ['Nik', 34, 'He said "eat cake and be happy!"'], ['Kate', 33, "Life is what happens when you're busy making other plans."], ] with open('listtocsv.csv', 'w') as file: writer = csv.writer(file) writer.writerow(headers) writer.writerows(values)

This returns the following CSV file:

Name,Age,Quote Nik,34,"He said ""eat cake and be happy!""" Kate,33,Life is what happens when you're busy making other plans. 

We can see that the character that contains a " in itself is escaped using the double quotes.

There are two ways in which this behavior can be customized:

Let’s see how we can disable the functionality of double quoting and use a custom escape character:

# Customizing the Escape Character import csv headers = ['Name', 'Age', 'Quote'] values = [ ['Nik', 34, 'He said "eat cake and be happy!"'], ['Kate', 33, "Life is what happens when you're busy making other plans."], ] with open('listtocsv.csv', 'w') as file: writer = csv.writer(file, doublequote=False, escapechar='/') writer.writerow(headers) writer.writerows(values)

This returns the following CSV file:

Name,Age,Quote Nik,34,He said /"eat cake and be happy!/" Kate,33,Life is what happens when you're busy making other plans. 

In the following section, you’ll learn how to write non-ASCII characters to a CSV file in Python.

How to Write UTF-8 Encoding to a CSV in Python

In order to write CSV files that are encoded by UTF-8, you can simply declare the encoding when you open the file. Because this operation is done before creating a writer object, this process is the same for creating CSV files based on lists or dictionaries.

Let’s see how we can do this in Python:

# Using UTF-8 Encoding When Writing a CSV File in Python import csv headers = ['Name', 'Age', 'Website', 'Location'] values = [ , , ] with open('dicttocsv.csv', 'w', encoding='utf-8') as file: writer = csv.DictWriter(file, fieldnames=headers) writer.writeheader() writer.writerows(values)

In the example above, we used the same code as in previous examples. However, when we opened the file, we passed in encoding='utf-8' , which ensures that the file is opened (or in this case, created) with the correct encoding.

Conclusion

In this guide, you learned how to use Python to write to CSV files using both lists and dictionaries. The Python csv module provides significant flexibility in terms of writing to CSV files. By providing writer objects for both lists and dictionaries, the module lets you customize the output of your CSV file.

You first learned how to write lists to CSV files, including writing a single row, multiple rows, and adding headers. Then, you covered the same topics with Python dictionaries, as well as working with the additional options that the DictWriter class provides. Finally, you learned how to customize behavior by exploring different options such as modifying encoding, changing delimiters and double quoting, as well as creating dialects for easier reusability.

Additional Resources

To learn more about related topics, check out the resources below:

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts