Write Data to CSV file using Python

Here’s an example code in Python to write data to a CSV file

import csv

# Define the data to be written
data = [
    ['Name', 'Age', 'Gender'],
    ['John', 25, 'Male'],
    ['Sarah', 30, 'Female'],
    ['David', 22, 'Male']
]

# Define the file path and name for the CSV file
file_path = 'data.csv'

# Open the CSV file in write mode
with open(file_path, mode='w', newline='') as csv_file:

    # Create a CSV writer object
    writer = csv.writer(csv_file)

    # Write the data to the CSV file row by row
    for row in data:
        writer.writerow(row)

print(f'Data has been written to {file_path} successfully.')

This code defines a 2D list of data to be written to a CSV file, sets the file path and name for the CSV file, opens the CSV file in write mode, creates a CSV writer object, and writes the data to the CSV file row by row using the writerow() method. Finally, it prints a message to indicate that the data has been written successfully.

Pamai Tech
Turning ideas into Reality

Products

Office Add-in

Enterprise Solutions

Cloud Consulting

UI UX Design

Data Transformation

Services

FAQ's

Privacy Policy

Terms & Condition

Team

Contact Us

Company

About Us

Services

Features

Our Pricing

Latest News

© 2025 Pamai Tech