Working with CSV & JSON Data
Checking access...
CSV Files
import csv
# Reading CSVwith open("data.csv", "r") as f: reader = csv.DictReader(f) for row in reader: print(row["name"], row["age"])
# Writing CSVwith open("output.csv", "w", newline="") as f: writer = csv.DictWriter(f, fieldnames=["name", "age", "email"]) writer.writeheader() writer.writerow({"name": "Alice", "age": 30, "email": "alice@example.com"})
# Custom delimiterwith open("data.tsv", "r") as f: reader = csv.DictReader(f, delimiter="\t")JSON
import json
# Readingwith open("data.json") as f: data = json.load(f)
# Writing with formattingwith open("data.json", "w") as f: json.dump(data, f, indent=2, sort_keys=True)
# Serialize custom objectsfrom datetime import datetime
def serialize(obj): if isinstance(obj, datetime): return obj.isoformat() raise TypeError(f"Type {type(obj)} not serializable")
data = {"time": datetime.now()}json_str = json.dumps(data, default=serialize, indent=2)SQLite Databases
import sqlite3
# Connect (creates file if not exists)conn = sqlite3.connect("app.db")cursor = conn.cursor()
# Create tablecursor.execute(""" CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, email TEXT UNIQUE, age INTEGER )""")
# Insertcursor.execute( "INSERT INTO users (name, email, age) VALUES (?, ?, ?)", ("Alice", "alice@example.com", 30),)conn.commit()
# Querycursor.execute("SELECT * FROM users WHERE age > ?", (25,))for row in cursor.fetchall(): print(f"ID: {row[0]}, Name: {row[1]}")
# Using row factory for named columnsconn.row_factory = sqlite3.Rowcursor = conn.cursor()cursor.execute("SELECT * FROM users")for row in cursor.fetchall(): print(row["name"], row["email"])
# Closeconn.close()HTTP Requests with urllib
from urllib.request import urlopen, Requestfrom urllib.parse import urlencodeimport json
# GET requestresponse = urlopen("https://api.github.com/users/python")data = json.loads(response.read())print(data["login"])
# GET with paramsparams = urlencode({"q": "python", "page": 1})response = urlopen(f"https://api.github.com/search/repositories?{params}")
# POST requestreq = Request( "https://httpbin.org/post", data=json.dumps({"key": "value"}).encode(), headers={"Content-Type": "application/json"},)response = urlopen(req)print(response.status) # 200Working with APIs (requests library)
import requests
# GETresponse = requests.get("https://api.github.com/users/python")data = response.json()print(data["login"], data["public_repos"])
# POSTresponse = requests.post( "https://httpbin.org/post", json={"key": "value"}, headers={"Authorization": "Bearer token123"},)print(response.status_code) # 201print(response.json())
# Error handlingtry: response = requests.get("https://api.github.com/users/nonexistent12345") response.raise_for_status() # Raises for 4xx/5xxexcept requests.exceptions.HTTPError as e: print(f"HTTP error: {e}")except requests.exceptions.ConnectionError: print("Connection failed")except requests.exceptions.Timeout: print("Request timed out")
# Session with connection poolingsession = requests.Session()session.headers.update({"User-Agent": "MyApp/1.0"})
response = session.get("https://httpbin.org/headers")print(response.json())Key Takeaways
- CSV:
csv.DictReader/csv.DictWriterfor structured data - JSON:
json.load()/json.dump()withindentanddefaultparameters - SQLite:
sqlite3with parameterized queries (?placeholders) - HTTP:
requestslibrary is simpler thanurllibfor API calls - Use sessions (
requests.Session()) for connection reuse - Always handle HTTP errors with
raise_for_status()