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:
- How to write CSV files in Python using the
csv.writer()
class andcsv.DictWriter()
class - How to write Python lists and dictionaries to CSV files
- How to handle common complexities, such as double quotes, non-ASCII characters, and escape characters
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)
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:
.writerow()
which writes a single row at a time. This can be useful to insert a header row into our resulting CSV file..writerows()
is used to write multiple rows at one time. This can be useful to insert data into a file.
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:
- We import the
csv
module - 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 aliasfile
to use the file. - We then create a
writer
object by passing this new file intocsv.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:
- Import the
csv
module - Create a single or multiple list variables
- Open a CSV file in write mode
- Instantiate a
csv.writer()
object by passing in the file as its argument - 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:
- Import the
csv
module - Create a list of lists that contains each row as a separate list
- Open a CSV file in write mode
- Instantiate a
csv.writer()
object by passing in the file as its argument - 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:
- First wrote the header row using the
.writerow()
method. - 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.
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 = {'Name': 'Nik', 'Age': 34, 'Website': 'datagy.io'}
kate = {'Website': 'google', 'Name': 'Kate', 'Age': 33}
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:
- Import the
csv
module - Declare a list of strings that contain the header row
- Create a dictionary containing the headers as keys. The order in the dictionary doesn’t matter.
- Create a CSV file and pass it into the
csv.DictWriter()
class to instantiate a writer object. Pass in the headers into thefieldnames=
parameter. - 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 = [
{'Name': 'Nik', 'Age': 34, 'Website': 'datagy.io'},
{'Website': 'google', 'Name': 'Kate', 'Age': 33},
{'Name': 'Evan', 'Age': 32, 'Website': 'bing'},
{'Name': 'Kyra', 'Age': 35, 'Website': 'yahoo'}
]
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 = [
{'Name': 'Nik', 'Age': 34, 'Website': 'datagy.io'},
{'Website': 'google', 'Name': 'Kate', 'Age': 33},
{'Name': 'Evan', 'Age': 32, 'Website': 'bing'},
{'Name': 'Kyra', 'Age': 35, 'Website': 'yahoo'}
]
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 = [
{'Name': 'Nik', 'Age': 34, 'Website': 'datagy.io'},
{'Website': 'google', 'Name': 'Kate', 'Age': 33},
{'Name': 'Evan', 'Age': 32, 'Website': 'bing'},
{'Name': 'Kyra', 'Age': 35, 'Website': 'yahoo'}
]
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:
restval=
specifies a value to write when a key is missing from a dictionaryextrasaction=
indicates what action to take when a key is included that doesn’t exist in the field names parameter. By default, the argument is set to'raise'
which will raise aTypeError
if an extra field is included. You can also set this to'ignore'
.
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 = [
{'Name': 'Nik', 'Age': 34, 'Website': 'datagy.io'},
{'Website': 'google', 'Name': 'Kate', 'Age': 33},
]
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:
quotechar=
customizes whether the quote character should be displayed. By default, it’s set toTrue
. When set toFalse
, the escape character is used. When we set it toFalse
, we need to pass in anescapechar=
.escapechar=
allows you to customize the escape character. This is necessary when thequotechar=
character is set toFalse
.
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 = [
{'Name': 'Nik', 'Age': 34, 'Website': 'datagy.io'},
{'Website': 'google', 'Name': 'Kate', 'Age': 33},
]
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: