DEV Community

Cover image for Day 44/100 – Working with CSV Files in Python
 Rahul Gupta
Rahul Gupta

Posted on

Day 44/100 – Working with CSV Files in Python

Welcome back to 100 Days, 100 Python Topics! Yesterday, we explored file modes and error handling. Today, we’ll focus on CSV files — a very common format for storing tabular data. Whether you’re dealing with spreadsheets, datasets, or exporting data from an app, Python’s csv module makes working with CSV files a breeze.


1. What is a CSV File?

A CSV (Comma-Separated Values) file is a plain text file where each line represents a row of data, and each value in the row is separated by a comma (or sometimes other delimiters like ; or \t).

Example:

Name,Age,Country John,25,USA Emma,30,UK Liam,28,Canada 
Enter fullscreen mode Exit fullscreen mode

2. Reading CSV Files

Python provides the built-in csv module to read CSV files easily.

import csv # Reading a CSV file with open('data.csv', mode='r') as file: reader = csv.reader(file) for row in reader: print(row) 
Enter fullscreen mode Exit fullscreen mode

Notes:

  • csv.reader() returns each row as a list of strings.
  • The first row often contains headers, which you might want to skip using next(reader).

Skipping the header example:

with open('data.csv', 'r') as file: reader = csv.reader(file) next(reader) # Skip the first row  for row in reader: print(row) 
Enter fullscreen mode Exit fullscreen mode

3. Writing to CSV Files

To create or modify a CSV file, use csv.writer().

import csv # Writing to a CSV file data = [ ["Name", "Age", "Country"], ["John", 25, "USA"], ["Emma", 30, "UK"] ] with open('output.csv', mode='w', newline='') as file: writer = csv.writer(file) writer.writerows(data) 
Enter fullscreen mode Exit fullscreen mode

Key points:

  • newline='' prevents adding extra blank lines on some systems.
  • Use writer.writerow() for a single row, writer.writerows() for multiple rows.

4. Using DictReader and DictWriter

Instead of working with lists, DictReader and DictWriter let you use column headers as keys.

Reading CSV as dictionaries:

import csv with open('data.csv', mode='r') as file: reader = csv.DictReader(file) for row in reader: print(row['Name'], row['Country']) 
Enter fullscreen mode Exit fullscreen mode

Writing CSV from dictionaries:

import csv data = [ {"Name": "John", "Age": 25, "Country": "USA"}, {"Name": "Emma", "Age": 30, "Country": "UK"} ] with open('dict_output.csv', mode='w', newline='') as file: fieldnames = ["Name", "Age", "Country"] writer = csv.DictWriter(file, fieldnames=fieldnames) writer.writeheader() writer.writerows(data) 
Enter fullscreen mode Exit fullscreen mode

5. Handling Different Delimiters

Not all CSVs use commas. Some use semicolons or tabs.

import csv with open('semicolon_data.csv', mode='r') as file: reader = csv.reader(file, delimiter=';') for row in reader: print(row) 
Enter fullscreen mode Exit fullscreen mode

6. Best Practices

✅ Always use with open() for automatic file closing.
✅ Use newline='' when writing to avoid extra blank lines.
✅ Consider pandas for large datasets.
✅ Handle encoding (e.g., encoding='utf-8') if your CSV contains special characters.


Challenge for You

  1. Create a CSV file containing at least 5 rows of employee data.
  2. Write a script to:
  • Read the CSV file using DictReader.
  • Add a new column for "Salary".
  • Save it back to a new CSV file using DictWriter.

Top comments (0)