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

</>
Copy
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:

  1. We import the csv module.
  2. We open a CSV file named example1.csv in write mode using open() with mode="w".
  3. The newline="" argument ensures that no extra blank lines appear.
  4. We create a CSV writer object using csv.writer(file).
  5. 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

</>
Copy
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:

  1. We define a list of lists called data that contains multiple rows.
  2. We open a file named example2.csv in write mode.
  3. We create a writer object using csv.writer(file).
  4. 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

</>
Copy
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:

  1. We define a list of dictionaries, where each dictionary represents a row.
  2. We open a CSV file in write mode.
  3. We define column names in fieldnames.
  4. We create a writer object using csv.DictWriter(file, fieldnames=fieldnames).
  5. We write headers using writeheader().
  6. 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

</>
Copy
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:

  1. We define a list of new rows to append.
  2. We open the existing CSV file in append mode using mode="a".
  3. We create a writer object using csv.writer(file).
  4. 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:

  1. writer.writerow() – Writes a single row.
  2. writer.writerows() – Writes multiple rows at once.
  3. DictWriter – Writes dictionary-based data.
  4. append mode (mode="a") – Appends data to an existing file.