codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐍

Python Programming

46 / 68 topics
45Python File Handling (Read/Write/Delete)46Reading & Writing CSV Files47Python Exception Handling48Python Custom Exceptions
Tutorials/Python Programming/Reading & Writing CSV Files
🐍Python Programming

Reading & Writing CSV Files

Updated 2026-04-20
2 min read

Introduction

CSV (Comma-Separated Values) is a widely used file format for storing tabular data, making it an essential skill for any programmer dealing with data manipulation and analysis. In this tutorial, we will explore how to read from and write to CSV files using Python's built-in csv module. We'll cover various scenarios, including handling different delimiters, managing headers, and working with large datasets.

Prerequisites

Before diving into the code examples, ensure you have Python installed on your system. The csv module is part of Python's standard library, so no additional installation is required.

Reading CSV Files

Basic CSV Reading

To read a CSV file, we can use the csv.reader() function. This function returns an iterator that produces lists corresponding to each row in the CSV file.

import csv

# Open the CSV file for reading
with open('example.csv', mode='r') as file:
    # Create a CSV reader object
    csv_reader = csv.reader(file)
    
    # Iterate over each row in the CSV file
    for row in csv_reader:
        print(row)

Reading with Headers

If your CSV file has headers, you can use csv.DictReader() to read the data into dictionaries. This makes it easier to access columns by their names.

import csv

# Open the CSV file for reading
with open('example.csv', mode='r') as file:
    # Create a DictReader object
    csv_reader = csv.DictReader(file)
    
    # Iterate over each row in the CSV file
    for row in csv_reader:
        print(row['Name'], row['Age'])

Handling Different Delimiters

By default, csv.reader() and csv.DictReader() use a comma as the delimiter. However, you can specify a different delimiter using the delimiter parameter.

import csv

# Open the CSV file for reading with a semicolon delimiter
with open('example.csv', mode='r') as file:
    # Create a CSV reader object with a semicolon delimiter
    csv_reader = csv.reader(file, delimiter=';')
    
    # Iterate over each row in the CSV file
    for row in csv_reader:
        print(row)

Writing CSV Files

Basic CSV Writing

To write data to a CSV file, use csv.writer(). This function returns a writer object that converts user data into delimited strings on the given file-like object.

import csv

# Data to be written to the CSV file
data = [
    ['Name', 'Age', 'City'],
    ['Alice', 30, 'New York'],
    ['Bob', 25, 'Los Angeles']
]

# Open the CSV file for writing
with open('output.csv', mode='w', newline='') as file:
    # Create a CSV writer object
    csv_writer = csv.writer(file)
    
    # Write data to the CSV file
    for row in data:
        csv_writer.writerow(row)

Writing with Headers

To write data with headers, use csv.DictWriter(). This function allows you to specify the fieldnames and write dictionaries to the CSV file.

import csv

# Data to be written to the CSV file
data = [
    {'Name': 'Alice', 'Age': 30, 'City': 'New York'},
    {'Name': 'Bob', 'Age': 25, 'City': 'Los Angeles'}
]

# Open the CSV file for writing
with open('output.csv', mode='w', newline='') as file:
    # Create a DictWriter object with fieldnames
    csv_writer = csv.DictWriter(file, fieldnames=['Name', 'Age', 'City'])
    
    # Write the header row
    csv_writer.writeheader()
    
    # Write data rows
    for row in data:
        csv_writer.writerow(row)

Writing Different Delimiters

Similar to reading, you can specify a different delimiter when writing using the delimiter parameter.

import csv

# Data to be written to the CSV file
data = [
    ['Name', 'Age', 'City'],
    ['Alice', 30, 'New York'],
    ['Bob', 25, 'Los Angeles']
]

# Open the CSV file for writing with a semicolon delimiter
with open('output.csv', mode='w', newline='') as file:
    # Create a CSV writer object with a semicolon delimiter
    csv_writer = csv.writer(file, delimiter=';')
    
    # Write data to the CSV file
    for row in data:
        csv_writer.writerow(row)

Best Practices

  1. Use Context Managers: Always use with statements when opening files to ensure that files are properly closed after their suite finishes.
  2. Handle Exceptions: Use try-except blocks to handle potential exceptions, such as FileNotFoundError or csv.Error.
  3. Specify Encoding: When dealing with non-ASCII characters, specify the encoding (e.g., encoding='utf-8') when opening files.
  4. Validate Data: Before writing data to a CSV file, validate it to ensure that it meets your requirements.

Conclusion

Reading and writing CSV files in Python is straightforward thanks to the built-in csv module. By following best practices and understanding how to handle different scenarios, you can efficiently manage data in CSV format for various applications.

Feel free to experiment with these examples and adapt them to fit your specific needs. Happy coding!


PreviousPython File Handling (Read/Write/Delete)Next Python Exception Handling

Recommended Gear

Python File Handling (Read/Write/Delete)Python Exception Handling