Python CSV & JSON File Processing
CSV (Comma-Separated Values) is the standard format for tabular spreadsheet data across Excel, Google Sheets, databases, and data science pipelines.
Python ships with the built-in csv module, which automatically handles escaped quotation marks, embedded commas in text, and platform newline quirks:
Always specify
newline="" when opening files for writing with the CSV module (open("data.csv", "w", newline="", encoding="utf-8")). This prevents Python from inserting blank empty rows on Windows operating systems!
import csv
# 1. Writing tabular records to a CSV file:
student_data = [
["Roll No", "Student Name", "Grade", "Percentage"],
[101, "Balaji", "A+", 94.5],
[102, "Alex Smith", "A", 88.0],
[103, "Chloe Davis", "A+", 96.2],
[104, "David Brown", "B", 72.4]
]
with open("students.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
writer.writerows(student_data)
print("โ
students.csv written successfully!")
# 2. Reading tabular records back with csv.reader:
print("\n--- Reading students.csv ---")
with open("students.csv", "r", encoding="utf-8") as f:
reader = csv.reader(f)
header = next(reader) # Extract first row as column header
print("Columns Header:", header)
for row in reader:
print(f"Roll: {row[0]:4} | Name: {row[1]:12} | Grade: {row[2]} ({row[3]}%)")
writer.writerows(list_of_lists)writes all 2D table rows in one efficient batch call.next(reader)advances the iterator by 1 row, cleanly stripping out the header before looping over data rows.
Accessing CSV columns by numeric indexes (like row[1]) is brittle: if a developer adds a new column, all indexes shift and break your code. csv.DictReader and csv.DictWriter map rows directly to Python dictionaries using header keys:
import csv
# 1. Writing CSV using Dictionary rows (DictWriter):
fieldnames = ["product_id", "product_name", "category", "price"]
products = [
{"product_id": "P01", "product_name": "Mechanical Keyboard", "category": "Hardware", "price": 2499.00},
{"product_id": "P02", "product_name": "Wireless Mouse", "category": "Hardware", "price": 799.00},
{"product_id": "P03", "product_name": "USB-C Hub", "category": "Accessories", "price": 1299.00}
]
with open("inventory.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader() # Automatically writes the first header row!
writer.writerows(products)
# 2. Reading CSV as clean dictionaries (DictReader):
print("--- Inventory Catalog (DictReader) ---")
with open("inventory.csv", "r", encoding="utf-8") as f:
reader = csv.DictReader(f)
for row in reader:
print(f"[{row['product_id']}] {row['product_name']:20} | Rs.{float(row['price']):.2f}")
You access fields by explicit name: row['product_name']. Even if columns in the CSV are reordered, your code continues functioning perfectly without changes!
While CSV is great for flat tabular data, modern web applications, configurations, and nested data schemas use JSON (JavaScript Object Notation).
json.dump(obj, file_stream, indent=4): Serializes Python dictionaries/lists directly to a disk file stream.json.load(file_stream): Reads and parses a JSON file directly into native Python dictionaries and lists.
import json
# Nested hierarchical application configuration:
app_settings = {
"app_name": "ManaCompiler Pro",
"version": "3.5.0",
"server": {
"host": "0.0.0.0",
"port": 8080,
"ssl_enabled": True
},
"features": ["code_editor", "realtime_compiler", "ai_code_review"],
"supported_languages": ["Python", "Java", "C", "JavaScript"]
}
# 1. Save complex dictionary to physical JSON file on disk:
with open("app_config.json", "w", encoding="utf-8") as f:
json.dump(app_settings, f, indent=4)
print("โ
app_config.json saved to disk!")
# 2. Read JSON file back from disk into Python dictionary:
with open("app_config.json", "r", encoding="utf-8") as f:
loaded_config = json.load(f)
print("\n--- Loaded JSON Config ---")
print("App Name: ", loaded_config["app_name"])
print("Host URL: ", f"http://{loaded_config['server']['host']}:{loaded_config['server']['port']}")
print("Languages:", ", ".join(loaded_config["supported_languages"]))
- Use CSV for 2D flat tables (spreadsheets, bank statements, sensor time series).
- Use JSON for nested hierarchical trees (user profiles with sub-arrays, API requests, configuration files).
Omitting newline="" when calling open("data.csv", "w") causes the Windows standard C runtime to write \r\r\n line endings, resulting in unwanted blank empty rows between every record in Excel. Always pass newline="".
Create a list of dictionaries representing 3 books (title, author, price). Save them to a CSV file named "books.csv" using DictWriter and print the file contents.
import csv
books = [
{"title": "Clean Code", "author": "Robert C. Martin", "price": 450},
{"title": "Fluent Python", "author": "Luciano Ramalho", "price": 890},
{"title": "The Pragmatic Programmer", "author": "Andrew Hunt", "price": 550}
]
with open("books.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=["title", "author", "price"])
writer.writeheader()
writer.writerows(books)
print("Saved books.csv successfully! Reading back:")
with open("books.csv", "r", encoding="utf-8") as f:
print(f.read().strip())
Q What is the difference between json.dumps() and json.dump()?
json.dumps(obj) converts a Python object into a string in RAM. json.dump(obj, file) writes the serialized JSON directly into an open file stream on disk.
Q How do I handle CSV files that use semicolons (;) or tabs (\t) instead of commas?
Pass delimiter=";" or delimiter="\t" into csv.reader() or csv.writer(): csv.reader(f, delimiter=";").
Q Can CSV files store nested objects or lists?
No. The standard CSV format is strictly 2D flat tabular text. To store nested arrays or dictionaries, use JSON, YAML, or SQLite.