CSV Handling

CSV (Comma Separated Values) is a simple and widely used format to store tabular data such as spreadsheets, database exports, reports, or logs. Python provides built-in support for reading and writing CSV files through the csv module. The module helps parse CSV files correctly, handle delimiters, manage quoting, and work with both simple and complex datasets.

CSV files are easy to create and use, making them a common choice in data processing, analysis, machine learning, and automation tasks.

1. Introduction to the csv Module

The csv module in Python offers tools to read from and write to CSV files. It supports:

  • Custom delimiters
  • Headers
  • Proper handling of quoted fields
  • Reading rows as lists or dictionaries
  • Writing structured data

The module focuses on correct CSV parsing rather than plain string splitting.

2. Reading CSV Files (csv.reader)

The csv.reader function reads data line-by-line and returns each row as a list of values.

Example CSV file (data.csv)

Name,Age,City
Aarav,21,Delhi
Meera,19,Mumbai

Reading the file

import csv

with open("data.csv") as f:
   reader = csv.reader(f)
   for row in reader:
       print(row)

Output:

['Name', 'Age', 'City']
['Aarav', '21', 'Delhi']
['Meera', '19', 'Mumbai']

Each row becomes a Python list.

3. Skipping the Header Row

Often we want to skip the first row (header).

import csv

with open("data.csv") as f:
   reader = csv.reader(f)
   next(reader)
   for row in reader:
       print(row)

Output:

['Aarav', '21', 'Delhi']
['Meera', '19', 'Mumbai']

4. Reading CSV as Dictionaries (csv.DictReader)

DictReader maps each row to a dictionary using the header names.

import csv

with open("data.csv") as f:
   reader = csv.DictReader(f)
   for row in reader:
       print(row)

Output:

{'Name': 'Aarav', 'Age': '21', 'City': 'Delhi'}
{'Name': 'Meera', 'Age': '19', 'City': 'Mumbai'}

Using a dictionary avoids index-based access and improves readability.

5. Writing to a CSV File (csv.writer)

The csv.writer is used to write rows to a CSV file.

import csv

with open("output.csv", "w", newline="") as f:
   writer = csv.writer(f)
   writer.writerow(["Name", "Age", "City"])
   writer.writerow(["Rahul", 22, "Chennai"])
   writer.writerow(["Neha", 20, "Kolkata"])

Output in output.csv:

Name,Age,City
Rahul,22,Chennai
Neha,20,Kolkata

newline="" prevents blank lines on Windows.

6. Writing CSV Using Dictionaries (csv.DictWriter)

DictWriter writes rows using dictionary keys.

import csv

with open("students.csv", "w", newline="") as f:
   fieldnames = ["Name", "Age", "City"]
   writer = csv.DictWriter(f, fieldnames=fieldnames)

   writer.writeheader()
   writer.writerow({"Name": "Rohan", "Age": 23, "City": "Pune"})
   writer.writerow({"Name": "Sara", "Age": 21, "City": "Bangalore"})

Output:

Name,Age,City
Rohan,23,Pune
Sara,21,Bangalore

DictWriter makes the code cleaner when working with structured data.

7. Custom Delimiters

CSV files do not always use commas. Some use semicolons, pipes, or tabs.

Example: Using a semicolon delimiter

import csv

with open("semicolon.csv") as f:
   reader = csv.reader(f, delimiter=";")
   for row in reader:
       print(row)

Example: Writing with a pipe delimiter

import csv

with open("pipe.csv", "w", newline="") as f:
   writer = csv.writer(f, delimiter="|")
   writer.writerow(["A", "B", "C"])

8. Handling Quotes and Special Characters

CSV files may contain commas in the data. In those cases, values must be quoted.

Example

Name,Comment
Aarav,"Hello, how are you?"

Python handles this correctly using quoting controls.

import csv

with open("comments.csv") as f:
   reader = csv.reader(f)
   for row in reader:
       print(row)

9. Reading Large CSV Files (Efficiently)

CSV files containing millions of rows should be processed line-by-line to avoid memory issues.

import csv

with open("large.csv") as f:
   reader = csv.reader(f)
   for row in reader:
       pass

The csv module streams data and does not load the whole file at once.

10. Converting CSV to Lists, Tuples, and JSON

Convert CSV to list of lists

import csv

with open("data.csv") as f:
   rows = list(csv.reader(f))
print(rows)

Convert CSV to JSON

import csv, json

with open("data.csv") as f:
   reader = csv.DictReader(f)
   data = list(reader)

with open("data.json", "w") as f:
   json.dump(data, f, indent=4)

11. Basic Error Handling

import csv

try:
   with open("unknown.csv") as f:
       reader = csv.reader(f)
       for row in reader:
           print(row)
except FileNotFoundError:
   print("File not found")

12. Summary Table

FunctionPurpose
csv.reader()Read CSV rows as lists
csv.DictReader()             Read CSV rows as dictionaries
csv.writer()Write CSV rows as lists
csv.DictWriter()Write CSV rows as dictionaries
delimiter=Change CSV delimiter
writerow()Write a single row
writerows()Write multiple rows
writeheader()Write column names