CSV (Comma-Separated Values) files are a common way of storing and exchanging tabular data. Python’s csv
module makes it easy to write data to CSV files. Whether you're exporting data for reporting, sharing datasets, or saving results from your program, understanding how to write to CSV files is essential.
In this guide, we will cover:
A CSV file is a plain-text file used to store tabular data. Each row in the file represents a record, and the values are separated by commas (or other delimiters). A simple CSV file might look like this:
Name, Age, Occupation
John, 28, Software Engineer
Jane, 34, Data Scientist
Doe, 45, Project Manager
Python provides several ways to write data to CSV files, primarily through the csv.writer()
and csv.DictWriter()
classes. Let's explore these methods in detail.
csv.writer()
The csv.writer()
function allows you to write rows of data to a CSV file. You can write data as lists of values, where each list corresponds to a row in the CSV file.
import csv
# Data to write
data = [
['Name', 'Age', 'Occupation'],
['John', 28, 'Software Engineer'],
['Jane', 34, 'Data Scientist'],
['Doe', 45, 'Project Manager']
]
# Open the file in write mode
with open('output.csv', 'w', newline='') as file:
writer = csv.writer(file)
# Write each row of data to the CSV file
writer.writerows(data)
csv.writer(file)
creates a writer object for the file.writer.writerows(data)
writes a list of rows (each row is itself a list of values).newline=''
parameter is used to ensure that there are no extra blank lines between rows, especially on Windows systems.Name,Age,Occupation
John,28,Software Engineer
Jane,34,Data Scientist
Doe,45,Project Manager
csv.DictWriter()
If you want to write data to a CSV file using dictionaries (where keys are column names), you can use csv.DictWriter()
. This is particularly useful when dealing with structured data.
DictWriter
import csv
# Data to write as a list of dictionaries
data = [
{'Name': 'John', 'Age': 28, 'Occupation': 'Software Engineer'},
{'Name': 'Jane', 'Age': 34, 'Occupation': 'Data Scientist'},
{'Name': 'Doe', 'Age': 45, 'Occupation': 'Project Manager'}
]
# Open the file in write mode
with open('output_with_header.csv', 'w', newline='') as file:
# Define the fieldnames (column names)
fieldnames = ['Name', 'Age', 'Occupation']
# Create a DictWriter object
writer = csv.DictWriter(file, fieldnames=fieldnames)
# Write the header (column names)
writer.writeheader()
# Write the rows (data)
writer.writerows(data)
csv.DictWriter(file, fieldnames=fieldnames)
creates a writer object that expects dictionaries with keys corresponding to the column names.writer.writeheader()
writes the column headers to the CSV file.writer.writerows(data)
writes each dictionary in the data
list to the CSV file.Name,Age,Occupation
John,28,Software Engineer
Jane,34,Data Scientist
Doe,45,Project Manager
By default, the csv.writer()
and csv.DictWriter()
functions use a comma (,
) as the delimiter between values. However, you can specify a custom delimiter if your CSV file uses a different character, such as a semicolon (;
).
import csv
# Data to write
data = [
['Name', 'Age', 'Occupation'],
['John', 28, 'Software Engineer'],
['Jane', 34, 'Data Scientist'],
['Doe', 45, 'Project Manager']
]
# Open the file in write mode with a semicolon delimiter
with open('semicolon_output.csv', 'w', newline='') as file:
writer = csv.writer(file, delimiter=';')
# Write each row to the CSV file
writer.writerows(data)
Name;Age;Occupation
John;28;Software Engineer
Jane;34;Data Scientist
Doe;45;Project Manager
In this example, we specify delimiter=';'
to write the CSV file with a semicolon as the delimiter.
In Python, CSV files are text-based, so all data written to a CSV file will be converted to strings. If you need to write numbers, dates, or other data types, you can use Python’s str()
function to explicitly convert the data to a string before writing.
import csv
import datetime
# Data with mixed data types (string, integer, datetime)
data = [
{'Name': 'John', 'Age': 28, 'Occupation': 'Software Engineer', 'Start Date': datetime.date(2020, 5, 1)},
{'Name': 'Jane', 'Age': 34, 'Occupation': 'Data Scientist', 'Start Date': datetime.date(2019, 8, 15)},
{'Name': 'Doe', 'Age': 45, 'Occupation': 'Project Manager', 'Start Date': datetime.date(2015, 3, 23)}
]
# Open the file in write mode
with open('mixed_data.csv', 'w', newline='') as file:
fieldnames = ['Name', 'Age', 'Occupation', 'Start Date']
writer = csv.DictWriter(file, fieldnames=fieldnames)
# Write the header
writer.writeheader()
# Write each row, ensuring the date is formatted as a string
for row in data:
row['Start Date'] = row['Start Date'].strftime('%Y-%m-%d') # Convert date to string
writer.writerow(row)
Start Date
from a datetime.date
object to a string using the strftime()
method, so it can be written to the CSV file.Name,Age,Occupation,Start Date
John,28,Software Engineer,2020-05-01
Jane,34,Data Scientist,2019-08-15
Doe,45,Project Manager,2015-03-23
CSV files may contain special characters like commas, newlines, or quotes. The csv
module automatically handles some of these, but there are cases where you might need to take extra care.
If your data contains quotes or newlines, the csv
module will escape these characters for you. For example:
import csv
# Data containing special characters
data = [
['Name', 'Age', 'Occupation'],
['John "The Coder"', 28, 'Software Engineer'],
['Jane', 34, 'Data Scientist\nwith a multiline description']
]
# Open the file in write mode
with open('special_characters.csv', 'w', newline='') as file:
writer = csv.writer(file)
# Write rows to the CSV file
writer.writerows(data)
Name,Age,Occupation
"John ""The Coder""",28,Software Engineer
Jane,34,"Data Scientist
with a multiline description"
In this example:
Name
field are escaped as ""
(double quotes).Occupation
field is properly handled by enclosing it in double quotes.When working with non-ASCII characters, such as accented letters or other languages, you may need to specify the encoding explicitly.
with open('utf8_output.csv', 'w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(['Name', 'Age', 'Occupation'])
writer.writerow(['José', 28, 'Software Engineer'])
By specifying encoding='utf-8'
, you ensure that non-ASCII characters are written correctly.
with
Statements: Always use a with
statement to open CSV files. It ensures proper closing of the file, even if an error occurs.utf-8
for non-ASCII characters) when working with international characters.DictWriter
for Structured Data: Use csv.DictWriter()
when your data is in dictionary form, especially for structured records with headers.