Tip
TL;DR (In 10 Seconds):
- JSON: Web data format (maps directly to Python Dictionaries & Lists).
- CSV: Tabular spreadsheet format (Comma-Separated Values).
dump()/load(): Used with FILE objects.dumps()/loads(): Used with STRING objects (sstands for String!).csv.DictReader&csv.DictWriter: Read and write CSVs using dictionaries instead of index positions.
JSON is the standard format for web APIs, configuration files, and database exports.
PYTHON DICTIONARY JSON TEXT FILE
┌──────────────────────┐ ┌──────────────────────┐
│ student = { │ dump │ { │
│ "name": "Rahul", │ ────────> │ "name": "Rahul", │
│ "marks": 90 │ <──────── │ "marks": 90 │
│ } │ load │ } │
└──────────────────────┘ └──────────────────────┘
json.dump(dict, f): Serializes Python dict into a JSON file.json.load(f): Deserializes JSON file into a Python dict.json.dumps(dict): Serializes Python dict into a JSON string.json.loads(str): Deserializes JSON string into a Python dict.
import json
payload = '{"service": "auth", "active": true, "ports": [8080, 8081]}'
# String deserialization
data = json.loads(payload)
print("Service Name:", data["service"])
print("First Port :", data["ports"][0])
# File serialization
with open("service.json", "w", encoding="utf-8") as f:
json.dump(data, f, indent=4, sort_keys=True)By default, json.dumps() raises TypeError if you pass non-standard objects like datetime or custom classes. Use the default parameter or custom JSONEncoder:
import json
from datetime import datetime
log_data = {
"event": "USER_LOGIN",
"timestamp": datetime.now()
}
# Pass default=str to convert non-serializable types into strings!
json_str = json.dumps(log_data, indent=4, default=str)
print(json_str)CSV files store spreadsheet tables as plaintext lines separated by delimiters (commas ,, semicolons ;, or tabs \t).
import csv
# 1. csv.reader (Returns lists of strings)
with open("data.csv", "r", encoding="utf-8") as f:
reader = csv.reader(f)
for row in reader:
print("Row List:", row) # ['Rahul', '85']
# 2. csv.DictReader (Returns dictionaries using headers as keys - RECOMMENDED!)
with open("data.csv", "r", encoding="utf-8") as f:
dict_reader = csv.DictReader(f)
for row in dict_reader:
print(f"Name: {row['Name']} | Score: {row['Score']}")
⚠️ CRITICAL RULE: Always open CSV files for writing withnewline=""! Otherwise, Windows will insert extra blank lines between rows.
import csv
users = [
{"username": "aarav_99", "role": "Admin", "level": 5},
{"username": "priya_dev", "role": "Developer", "level": 3}
]
# DictWriter automatically aligns dictionary values to specified fieldnames!
fieldnames = ["username", "role", "level"]
with open("users.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader() # Write column header row
writer.writerows(users)
print("✅ CSV written using DictWriter!")| Task | Method | Example |
|---|---|---|
| JSON Dict -> File | json.dump(dict, f) |
json.dump(data, f, indent=4) |
| JSON File -> Dict | json.load(f) |
data = json.load(f) |
| JSON Dict -> String | json.dumps(dict) |
str_val = json.dumps(data, default=str) |
| JSON String -> Dict | json.loads(str) |
data = json.loads(str_val) |
| CSV List -> File | csv.writer(f) |
writer.writerow(["A", "B"]) |
| CSV Dict -> File | csv.DictWriter(f, fieldnames=keys) |
writer.writeheader(); writer.writerows(dicts) |
# ❌ WRONG: Creates blank rows between lines on Windows!
# with open("out.csv", "w") as f: writer = csv.writer(f)
# ✅ RIGHT: Pass newline=""!
with open("out.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)Task: Write a Python program that creates a list of 2 dictionaries ({"id": 1, "status": "OK"}), writes them to status.csv using csv.DictWriter, then reads them back using csv.DictReader.
💡 Click to See Solution
import csv
items = [
{"id": "1", "status": "OK"},
{"id": "2", "status": "FAIL"}
]
# Write
with open("status.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=["id", "status"])
writer.writeheader()
writer.writerows(items)
# Read
with open("status.csv", "r", encoding="utf-8") as f:
for row in csv.DictReader(f):
print(f"ID {row['id']} -> {row['status']}")
# Output:
# ID 1 -> OK
# ID 2 -> FAIL