Open In App

How To Create A Csv File Using Python

Last Updated : 22 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

CSV stands for comma-separated values, it is a type of text file where information is separated by commas (or any other delimiter), they are commonly used in databases and spreadsheets to store information in an organized manner. In this article, we will see how we can create a CSV file using Python.

Create a Csv File Using Python

Below are some of the ways by which we can create a CSV file using Python:

  1. Using Python CSV Module
  2. Using Pandas Library
  3. Using plain text file writing

Using Python CSV Module

Now, Python provides a CSV module to work with CSV files, which allows Python programs to create, read, and manipulate tabular data in the form of CSV, This ‘CSV’ module provides many functions and classes for working with CSV files, It includes functions for reading and writing CSV data, as well as classes like csv.reader and csv.writer for more advanced operations.

Python3




# importing csv
import csv
 
# Data
data = [
    ['Name', 'Age', 'City'],
    ['Aman', 28, 'Pune'],
    ['Poonam', 24, 'Jaipur'],
    ['Bobby', 32, 'Delhi']
]
 
# File path for the CSV file
csv_file_path = 'example.csv'
 
# Open the file in write mode
with open(csv_file_path, mode='w', newline='') as file:
    # Create a csv.writer object
    writer = csv.writer(file)
    # Write data to the CSV file
    writer.writerows(data)
 
# Print a confirmation message
print(f"CSV file '{csv_file_path}' created successfully.")


Output:

Screenshot-2024-02-08-122743

Using Pandas Library

Pandas is a powerful Python library widely used for data manipulation and analysis, now Pandas also offers methods for converting data into CSV format and writing it to a file. In this example, we are using Pandas to create and edit CSV files in Python.

Python3




# Step 1 Importing pandas
import pandas as pd
 
# Step 2 Prepare your data
data = {
    'Name': ['Rajat', 'Tarun', 'Bobby'],
    'Age': [30, 25, 35],
    'City': ['New York', 'Delhi', 'Pune']
}
 
# Step 3 Create a DataFrame using DataFrame function
df = pd.DataFrame(data)
 
# Step 4 Specify the file path to save data
csv_file_path = 'data.csv'
 
# Step 5 Write the DataFrame to a CSV file using to_csv() function where file path is passed
df.to_csv(csv_file_path, index=False)
 
print(f'CSV file "{csv_file_path}" has been created successfully.')


Output:

Screenshot-2024-02-11-104520

File Creation Screenshot

Note: Ignore the deprecation warning message.

Csv File Output:

Screenshot-2024-02-11-102654

csv file

Using Plain Text File Writing

We can manually write data to a CSV file using basic file writing operations. While it is less common and less convenient than using the csv module or pandas, it’s still possible and can be done.

Python3




# data to be stored in csv in form of list of list
data = [
    ['Name', 'Gender', 'Age', 'Course'],
    ['Aman', 'M', 22, 'B.Tech'],
    ['Pankaj', 'M', 24, 'M.Tech'],
    ['Beena', 'F', '23', 'MBA']
]
 
# file path of csv to be stored
csv_file_path = 'ex3.csv'
 
# opening file in write mode using a context manager
with open(csv_file_path, mode='w') as file:
    for row in data:
        file.write(','.join(map(str, row)) + '\n'# writing data row by row
 
print(f"CSV file '{csv_file_path}' created successfully!!!")


Output:

Screenshot-2024-02-11-111509

csv file



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads