Skip to content

Latest commit

 

History

History
182 lines (135 loc) · 5.87 KB

File metadata and controls

182 lines (135 loc) · 5.87 KB

1. Working with JSON and CSV

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 (s stands for String!).
  • csv.DictReader & csv.DictWriter: Read and write CSVs using dictionaries instead of index positions.

1. Working with JSON (JavaScript Object Notation)

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    │ }                    │
 └──────────────────────┘           └──────────────────────┘

A. Core JSON Functions

  • 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)

B. Advanced JSON: Handling Non-Serializable Types (datetime)

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)

2. Working with CSV (Comma-Separated Values)

CSV files store spreadsheet tables as plaintext lines separated by delimiters (commas ,, semicolons ;, or tabs \t).

A. Reading CSV (csv.reader vs csv.DictReader)

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']}")

B. Writing CSV (csv.writer & csv.DictWriter)

⚠️ CRITICAL RULE: Always open CSV files for writing with newline=""! 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!")

3. Quick Reference Function Matrix

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)

4. ⚠️ Traps & Mistakes to Avoid

Trap 1: Missing newline="" in CSV writing

# ❌ 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)

🎯 Self-Check Practice Exercise

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