Skip to content

CSV FILE HANDLING - Reading and writing CSV data

Python
#!/usr/bin/env python3
"""
CSV FILE HANDLING - Reading and writing CSV data
Demonstrates working with comma-separated values files
"""

import os
import tempfile

print("=" * 60)
print("CSV FILE HANDLING - Comma-Separated Values")
print("=" * 60)

temp_dir = tempfile.gettempdir()

# Example 1: Writing simple CSV manually
print("\n1. Writing CSV Manually")
print("-" * 40)
csv_file = os.path.join(temp_dir, "employees.csv")

with open(csv_file, 'w') as f:
    # Header
    f.write("Name,Age,Department,Salary\n")
    # Data rows
    f.write("John Smith,35,Engineering,75000\n")
    f.write("Jane Doe,28,Marketing,65000\n")
    f.write("Bob Johnson,42,Sales,70000\n")
    f.write("Alice Williams,31,Engineering,80000\n")

print(f"Created: {csv_file}")

# Example 2: Reading CSV manually
print("\n2. Reading CSV Manually")
print("-" * 40)
with open(csv_file, 'r') as f:
    header = f.readline().strip()
    print(f"Header: {header}")
    print(f"Columns: {header.split(',')}")
    print("\nData:")
    for line in f:
        fields = line.strip().split(',')
        print(f"  {fields}")

# Example 3: Parsing CSV into structured data
print("\n3. Parsing CSV into Dictionaries")
print("-" * 40)
employees = []
with open(csv_file, 'r') as f:
    header = f.readline().strip().split(',')
    for line in f:
        values = line.strip().split(',')
        employee = {}
        for i, column in enumerate(header):
            employee[column] = values[i]
        employees.append(employee)

print(f"Loaded {len(employees)} employees:")
for emp in employees:
    print(f"  {emp['Name']}: {emp['Department']}, ${emp['Salary']}")

# Example 4: Writing CSV from data structure
print("\n4. Writing CSV from List of Dicts")
print("-" * 40)
products = [
    {"Product": "Laptop", "Price": "999", "Stock": "15"},
    {"Product": "Mouse", "Price": "25", "Stock": "50"},
    {"Product": "Keyboard", "Price": "75", "Stock": "30"},
]

products_csv = os.path.join(temp_dir, "products.csv")
with open(products_csv, 'w') as f:
    # Write header
    headers = list(products[0].keys())
    f.write(','.join(headers) + '\n')

    # Write data
    for product in products:
        values = [product[h] for h in headers]
        f.write(','.join(values) + '\n')

print(f"Created: {products_csv}")

# Verify
with open(products_csv, 'r') as f:
    print("Content:")
    print(f.read())

# Example 5: Filtering CSV data
print("\n5. Filtering CSV Data")
print("-" * 40)
high_earners_csv = os.path.join(temp_dir, "high_earners.csv")

with open(csv_file, 'r') as src, open(high_earners_csv, 'w') as dst:
    header = src.readline()
    dst.write(header)  # Copy header

    for line in src:
        fields = line.strip().split(',')
        salary = int(fields[3])
        if salary >= 70000:
            dst.write(line)

print(f"Created filtered file: {high_earners_csv}")
with open(high_earners_csv, 'r') as f:
    print("High earners (>=$70k):")
    f.readline()  # Skip header
    for line in f:
        print(f"  {line.strip()}")

# Example 6: CSV with quotes (handling commas in data)
print("\n6. Handling Commas in Data (Quoted Fields)")
print("-" * 40)
quoted_csv = os.path.join(temp_dir, "quoted.csv")

with open(quoted_csv, 'w') as f:
    f.write('Name,Description,Price\n')
    f.write('Widget,"Small, useful item",10\n')
    f.write('Gadget,"Large, expensive device",100\n')
    f.write('Tool,"Red, blue, or green",50\n')

print(f"Created: {quoted_csv}")
print("Raw content:")
with open(quoted_csv, 'r') as f:
    print(f.read())

print("Note: Proper CSV parsing needs csv module for quotes")

# Example 7: Appending to CSV
print("\n7. Appending New Records to CSV")
print("-" * 40)
with open(csv_file, 'a') as f:
    f.write("Charlie Brown,29,HR,60000\n")
    f.write("Diana Prince,33,Legal,85000\n")
    print("Added 2 new employees")

with open(csv_file, 'r') as f:
    lines = f.readlines()
    print(f"Total rows now: {len(lines)} (including header)")

# Example 8: CSV statistics
print("\n8. Computing CSV Statistics")
print("-" * 40)
total_salary = 0
count = 0
departments = {}

with open(csv_file, 'r') as f:
    f.readline()  # Skip header
    for line in f:
        fields = line.strip().split(',')
        salary = int(fields[3])
        dept = fields[2]

        total_salary += salary
        count += 1

        if dept not in departments:
            departments[dept] = 0
        departments[dept] += 1

print(f"Total employees: {count}")
print(f"Average salary: ${total_salary / count:,.2f}")
print(f"Employees by department:")
for dept, cnt in departments.items():
    print(f"  {dept}: {cnt}")

# Example 9: Creating summary report
print("\n9. Creating Summary Report")
print("-" * 40)
report_file = os.path.join(temp_dir, "salary_report.txt")

with open(csv_file, 'r') as csv_in, open(report_file, 'w') as report_out:
    report_out.write("SALARY REPORT\n")
    report_out.write("=" * 50 + "\n\n")

    csv_in.readline()  # Skip header
    for line in csv_in:
        fields = line.strip().split(',')
        name = fields[0]
        salary = int(fields[3])
        report_out.write(f"{name:20s} ${salary:>8,}\n")

with open(report_file, 'r') as f:
    print(f.read())

# Cleanup
for f in [csv_file, products_csv, high_earners_csv, quoted_csv, report_file]:
    if os.path.exists(f):
        os.remove(f)

print("\n" + "=" * 60)
print("Key Points:")
print("  - CSV = Comma-Separated Values")
print("  - split(',') for simple parsing")
print("  - Use csv module for complex CSVs")
print("  - Watch for commas in data (quotes)")
print("  - Great for tabular data")
print("=" * 60)