CSV, JSON, and Binary Files
CSV, JSON, and Binary Files
Section titled “CSV, JSON, and Binary Files”Introduction
Section titled “Introduction”Beyond plain text, Python excels at working with structured data formats like CSV and JSON, as well as binary files.
CSV Files
Section titled “CSV Files”import csv
# Reading CSVwith open("data.csv", "r") as f: reader = csv.reader(f) for row in reader: print(row) # ['Name', 'Age', 'City']
# Reading as dictionarieswith 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.writer(f) writer.writerow(["Name", "Age", "City"]) writer.writerow(["Alice", 30, "NYC"]) writer.writerow(["Bob", 25, "LA"])
# Writing as dictionarieswith open("output.csv", "w", newline="") as f: fields = ["Name", "Age", "City"] writer = csv.DictWriter(f, fieldnames=fields) writer.writeheader() writer.writerow({"Name": "Alice", "Age": 30, "City": "NYC"})JSON Files
Section titled “JSON Files”import json
# Python datadata = { "name": "Alice", "age": 30, "skills": ["Python", "SQL", "Docker"], "active": True, "address": None}
# Writing JSONwith open("data.json", "w") as f: json.dump(data, f, indent=2)
# Reading JSONwith open("data.json", "r") as f: loaded = json.load(f) print(loaded["name"]) # Alice
# JSON string <-> Pythonjson_str = json.dumps(data)parsed = json.loads(json_str)
# Pretty printingprint(json.dumps(data, indent=2, sort_keys=True))Binary Files
Section titled “Binary Files”# Write binarywith open("data.bin", "wb") as f: data = bytes([0x48, 0x65, 0x6C, 0x6C, 0x6F]) f.write(data)
# Read binarywith open("data.bin", "rb") as f: content = f.read() print(content) # b'Hello'Practice Exercises
Section titled “Practice Exercises”Exercise 1: Write a program that reads a CSV file of student grades and calculates averages.
Exercise 2: Create a JSON configuration manager that loads, validates, and saves config files.
Exercise 3: Implement a program that converts CSV data to JSON format.