CSV (Comma-Separated Values) is one of the most ubiquitous plain-text data interchange formats across software engineering, data science, and business analytics. Python provides robust, highly optimized native support for reading, writing, and parsing CSV documents through its built-in csv standard library module.
1. What is a CSV File?
A CSV file stores tabular data (numbers and text) in plain text form, where each line represents a data record (row) and each record consists of one or more fields separated by a delimiter character (most commonly a comma ,).
1id,name,role,department
2101,Aarav Sharma,Software Engineer,Engineering
3102,Priya Patel,Data Scientist,Analytics
4103,Rohan Verma,Product Manager,Product
While you could theoretically read a CSV using basic split(',') strings, commas frequently occur inside quoted fields (e.g., "New Delhi, India" or "Engineering, Platform"). Python's csv module automatically handles escaping, quotes, delimiters, and line endings.
2. Reading CSV Files with csv.reader
The csv.reader object takes an iterable file stream and yields each row as a list of strings.
next(reader): Moves the iterator past the header line so the loop only processes data records.
2
String Types: All extracted values are strings. You must explicitly convert numbers (int(row[0]) or float(row[3])).
3. Writing CSV Files with csv.writer
The csv.writer object provides methods to write sequences of data as formatted CSV rows: writerow() for a single row and writerows() for a collection of rows.
1import csv
2
3headers =["Name","Score","Grade"]
4students =[
5["Aarav",92,"A"],
6["Diya",85,"B"],
7["Kavya",97,"A+"]
8]
9
10# CRITICAL: Always use newline='' when opening files for writing with csv
Why newline='' is mandatory:
On Windows systems, default file writing translates \n to \r\n. Without newline='', the csv module adds its own \r\n, resulting in blank empty lines between every row. Specifying newline='' allows the csv module to control line terminators correctly across all platforms.
Working with lists relies on positional indexing (row[1]), which easily breaks if columns change order. Python's csv.DictReader and csv.DictWriter map CSV rows directly to Python dictionaries.
Reading with csv.DictReader
csv.DictReader treats the first row as field names (keys) and returns each subsequent row as an OrderedDict or standard dict.
csv.QUOTE_MINIMAL(default): Quotes fields only if they contain the delimiter, quote char, or line break.
csv.QUOTE_ALL: Quotes every single field regardless of content.
csv.QUOTE_NONNUMERIC: Quotes all string fields and automatically casts unquoted items to floats when reading.
csv.QUOTE_NONE: Never quotes fields; requires an escape character if the delimiter appears inside text.
6. Summary Comparison Table
Feature
csv.reader / csv.writer
csv.DictReader / csv.DictWriter
Data Format
List of strings (['101', 'Aarav'])
Dictionary ({'id': '101', 'name': 'Aarav'})
Indexing
Numeric index (row[0])
Column key (row['id'])
Refactor Safety
Low (column shifts break index)
High (columns can appear in any order)
Performance
Slightly faster, lower memory
Minor overhead for dict creation
Best For
Pure mathematical/raw data arrays
Structured records and entity objects
⚠️ Common Intermediate Mistakes & Gotchas
1. Forgetting newline='' on Windows
Opening files for CSV writing without newline='' causes empty blank rows between every line on Windows due to double carriage-return translation (\r\r\n):
All values read from csv.reader or csv.DictReader are plain strings. Always explicitly convert numbers before arithmetic:
1# ❌ INCORRECT: "50" + "10" = "5010"
2total = row["price"]+ row["tax"]
3
4# ✅ CORRECT
5total =float(row["price"])+float(row["tax"])
💻 Try It Yourself: Automated Employee Payroll Filter
Scenario
You are building an HR payroll processing script. Given a CSV file of employee salaries, write a script that reads the records, filters employees earning above ₹75,000, and writes their names, departments, and revised salaries (with an 8% bonus added) to a new CSV file bonus_payouts.csv.