Write to a CSV File using Python
To write data to a CSV file in Python, we use the built-in csv
module, which provides functions like csv.writer()
to write rows of data into a file. This tutorial explains different ways to write data to a CSV file with examples for beginners.
Examples for Writing to a CSV File
1. Writing a Simple List to a CSV File
In this example, we will write a list of values as a single row in a CSV file using the csv.writer()
method.
main.py
import csv
# Open a file in write mode
with open("example1.csv", mode="w", newline="") as file:
writer = csv.writer(file)
# Writing a single row
writer.writerow(["Name", "Age", "City"])
print("CSV file created successfully.")
Explanation:
- We import the
csv
module. - We open a CSV file named
example1.csv
in write mode usingopen()
withmode="w"
. - The
newline=""
argument ensures that no extra blank lines appear. - We create a CSV writer object using
csv.writer(file)
. - We use
writer.writerow()
to write a single row containing column headers.
Output:
CSV file created successfully.
The content of the created example1.csv
is:

2. Writing Multiple Rows to a CSV File
In this example, we will write multiple rows of data to a CSV file using the writerows()
method.
main.py
import csv
# Data to write
data = [
["Name", "Age", "City"],
["Alice", 25, "New York"],
["Bob", 30, "Los Angeles"],
["Charlie", 22, "Chicago"]
]
# Open file and write data
with open("example2.csv", mode="w", newline="") as file:
writer = csv.writer(file)
# Writing multiple rows
writer.writerows(data)
print("CSV file with multiple rows created successfully.")
Explanation:
- We define a list of lists called
data
that contains multiple rows. - We open a file named
example2.csv
in write mode. - We create a writer object using
csv.writer(file)
. - We use
writer.writerows(data)
to write multiple rows at once.
Output:
CSV file with multiple rows created successfully.
The content of the created example1.csv
is:

3. Writing Data Using Dictionary and DictWriter
In this example, we will write a CSV file using a dictionary where column headers are automatically assigned.
main.py
import csv
# List of dictionaries
data = [
{"Name": "Alice", "Age": 25, "City": "New York"},
{"Name": "Bob", "Age": 30, "City": "Los Angeles"},
{"Name": "Charlie", "Age": 22, "City": "Chicago"}
]
# Open file and write data using DictWriter
with open("example3.csv", mode="w", newline="") as file:
fieldnames = ["Name", "Age", "City"]
writer = csv.DictWriter(file, fieldnames=fieldnames)
# Writing headers
writer.writeheader()
# Writing rows
writer.writerows(data)
print("CSV file created using dictionary.")
Explanation:
- We define a list of dictionaries, where each dictionary represents a row.
- We open a CSV file in write mode.
- We define column names in
fieldnames
. - We create a writer object using
csv.DictWriter(file, fieldnames=fieldnames)
. - We write headers using
writeheader()
. - We use
writerows(data)
to write multiple rows using dictionaries.
Output:
CSV file created using dictionary.
The content of the created example1.csv
is:

4. Appending Data to an Existing CSV File
In this example, we will append new rows to an existing CSV file without overwriting its content. We shall use example3.csv
that we already created in the previous example.
main.py
import csv
# New data to append
new_data = [
["David", 28, "Miami"],
["Eve", 35, "Seattle"]
]
# Open file in append mode
with open("example3.csv", mode="a", newline="") as file:
writer = csv.writer(file)
# Appending rows
writer.writerows(new_data)
print("Data appended to the CSV file successfully.")
Explanation:
- We define a list of new rows to append.
- We open the existing CSV file in append mode using
mode="a"
. - We create a writer object using
csv.writer(file)
. - We use
writer.writerows(new_data)
to append rows without deleting existing content.
Output:
Data appended to the CSV file successfully.

Conclusion
Python provides multiple ways to write data to a CSV file. In this tutorial, we have covered:
writer.writerow()
– Writes a single row.writer.writerows()
– Writes multiple rows at once.DictWriter
– Writes dictionary-based data.append mode (mode="a")
– Appends data to an existing file.