Learn how to Reading and Writing CSV Files in Python using the CSV module. Includes syntax, examples with csv.reader, csv.writer, DictReader, and DictWriter explained.
Reading and Writing CSV Files in Python
Introduction
A CSV (Comma Separated Values) file is one of the most popular formats used to store and exchange data in tabular form. Each line in a CSV file represents a row of data, and values are separated by commas — similar to how data is structured in an Excel spreadsheet.
In Python, the csv module is used to read, write, and process CSV files easily. It is part of the Python Standard Library, meaning no external installation is needed.
What is the CSV Module?
The csv module in Python provides functions and classes to:
- Read CSV files line by line.
- Write data into CSV format.
- Work with data using lists or dictionaries.
It is especially useful for data analysis, machine learning preprocessing, and handling spreadsheet-style data.
Reading a CSV File
Syntax
import csv
with open(‘data.csv’, ‘r’) as file:
reader = csv.reader(file)
for row in reader:
print(row)
Explanation
- open(‘data.csv’, ‘r’) → Opens the CSV file in read mode.
- csv.reader(file) → Creates a CSV reader object to read data row by row.
- for row in reader: → Iterates through each row (returned as a list).
Example CSV File: data.csv
name,age,city
Rahul,21,Mumbai
Sneha,22,Pune
Output
[‘name’, ‘age’, ‘city’]
[‘Rahul’, ’21’, ‘Mumbai’]
[‘Sneha’, ’22’, ‘Pune’]
Writing a CSV File
Syntax
import csv
with open(‘student.csv’, ‘w’, newline=”) as file:
writer = csv.writer(file)
writer.writerow([‘name’, ‘age’, ‘city’])
writer.writerow([‘Rahul’, 21, ‘Mumbai’])
writer.writerow([‘Sneha’, 22, ‘Pune’])
Explanation
- open(‘student.csv’, ‘w’) → Opens the file in write mode.
- csv.writer(file) → Creates a writer object.
- writer.writerow() → Writes a single row of data.
Output (student.csv file)
name,age,city
Rahul,21,Mumbai
Sneha,22,Pune
Reading and Writing CSV Files in Python
Reading a CSV File as a Dictionary
You can also read a CSV file as a dictionary, where the column headers act as keys.
Example
import csv
with open(‘student.csv’, ‘r’) as file:
reader = csv.DictReader(file)
for row in reader:
print(row[‘name’], row[‘city’])
Output
Rahul Mumbai
Sneha Pune
Writing a CSV File as a Dictionary
The DictWriter() function allows you to write data using key-value pairs (dictionary format).
Example
import csv
with open(‘student_dict.csv’, ‘w’, newline=”) as file:
fieldnames = [‘name’, ‘age’, ‘city’]
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader() # write column headers
writer.writerow({‘name’: ‘Rahul’, ‘age’: 21, ‘city’: ‘Mumbai’})
writer.writerow({‘name’: ‘Sneha’, ‘age’: 22, ‘city’: ‘Pune’})
Output (student_dict.csv)
name,age,city
Rahul,21,Mumbai
Sneha,22,Pune
Key Points
- CSV stands for Comma Separated Values.
- CSV files store data in tabular (row-column) form.
- Use csv.reader() to read CSV files.
- Use csv.writer() to write data into CSV files.
- Use DictReader() and DictWriter() to handle CSV files in dictionary format.
- CSV files are lightweight, easy to share, and widely supported by databases and spreadsheet software.
Conclusion
The CSV module in Python provides a powerful and simple way to handle structured data.
It allows smooth interaction between Python programs, Excel sheets, and databases, making it essential for data analysis, automation, and reporting tasks.
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
https://defineinfoloop.blogspot.com/?m=1