Python CSV Files: Read, Write, and Process Data

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with CSV Files in Python

CSV (Comma Separated Values) is one of the most popular formats for storing tabular data. It's used everywhere—from exporting Excel spreadsheets to exchanging data between web services and databases.

Python offers powerful tools for handling CSV files. You can use the built-in csv module for basic tasks or the pandas library for advanced data analysis. This guide covers reading and writing CSV files, data processing, error prevention, and performance optimization for large datasets.

CSV Format Basics

CSV File Structure

CSV is a simple text format. Each line in the file represents a single record. Values within a record are separated by special characters. By default, a comma is used, but semicolons, tabs, or pipes are also common.

Here's what a standard CSV file looks like:

Name,Age,City
Anna,25,New York
John,30,Los Angeles

Advantages of CSV Format

CSV offers several key benefits:

  • Simple structure and human readability
  • Wide support across programs and programming languages
  • Small file size compared to other formats
  • Editable in any text editor
  • Universal for data exchange between different systems

Working with CSV Using the csv Module

Reading CSV Files Basics

Python's standard csv module is perfect for basic CSV operations. Here's how to read data simply:

import csv

with open("data.csv", newline='', encoding='utf-8') as f:
    reader = csv.reader(f)
    for row in reader:
        print(row)

Output:

['Name', 'Age', 'City']
['Anna', '25', 'New York']
['John', '30', 'Los Angeles']

Reading with Headers

For easy access to named columns, use csv.DictReader:

with open("data.csv", encoding='utf-8') as f:
    reader = csv.DictReader(f)
    for row in reader:
        print(row["Name"], row["City"])

This method automatically creates dictionaries for each row, with keys taken from the first line of the file.

Writing Data to a CSV File

Simple Row Writing

The basic way to write data uses csv.writer:

with open("output.csv", mode='w', newline='', encoding='utf-8') as f:
    writer = csv.writer(f)
    writer.writerow(["Name", "Age"])
    writer.writerow(["Oleg", 35])

Writing Dictionary Data

For structured data, csv.DictWriter is the way to go:

data = [
    {"Name": "Maria", "Age": 28},
    {"Name": "Alex", "Age": 40},
]

with open("people.csv", "w", newline='', encoding='utf-8') as f:
    writer = csv.DictWriter(f, fieldnames=["Name", "Age"])
    writer.writeheader()
    writer.writerows(data)

Configuring Read and Write Settings

The csv module lets you customize various parameters:

  • delimiter: specify the field separator
  • quotechar: character for quoting values
  • skipinitialspace: skip whitespace after the delimiter
  • lineterminator: line ending character

Working with CSV in pandas

Installing and Importing pandas

The pandas library offers more advanced CSV handling capabilities:

pip install pandas

Reading CSV Files in pandas

The main method for reading CSV files:


                    

Blogs

Book Recommendations