📊 Working with CSV Files

CSV (Comma-Separated Values) files are a common format for data exchange. Python's csv module provides powerful tools for reading and writing CSV data.

import csv

# Basic CSV reading
with open('data.csv', 'r') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

🎯 CSV Operations

Python offers several approaches to work with CSV files.

Reading CSV Files

import csv

# Reading CSV as lists
print("Reading as lists:")
with open('students.csv', 'r') as file:
    reader = csv.reader(file)
    header = next(reader)  # Get header row
    print(f"Header: {header}")
    
    for row in reader:
        print(f"Row: {row}")

print("CSV reading as lists completed")
# Reading CSV as dictionaries
print("Reading as dictionaries:")
with open('students.csv', 'r') as file:
    reader = csv.DictReader(file)
    for row in reader:
        print(f"Name: {row['name']}, Age: {row['age']}")

print("CSV reading as dictionaries completed")

Writing CSV Files

# Writing CSV from lists
students_data = [
    ['name', 'age', 'grade'],
    ['Alice', 20, 'A'],
    ['Bob', 19, 'B'],
    ['Charlie', 21, 'C']
]

with open('output.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerows(students_data)

print("CSV written from lists")
# Writing CSV from dictionaries
students_dict = [
    {'name': 'Alice', 'age': 20, 'grade': 'A'},
    {'name': 'Bob', 'age': 19, 'grade': 'B'},
    {'name': 'Charlie', 'age': 21, 'grade': 'C'}
]

with open('dict_output.csv', 'w', newline='') as file:
    fieldnames = ['name', 'age', 'grade']
    writer = csv.DictWriter(file, fieldnames=fieldnames)
    writer.writeheader()
    writer.writerows(students_dict)

print("CSV written from dictionaries")

🔍 Advanced CSV Handling

Python allows more sophisticated CSV operations.

Custom Delimiters and Quoting

import csv

# Writing with custom delimiter and quoting
with open('custom.csv', 'w', newline='') as file:
    writer = csv.writer(file, delimiter=';', quotechar='"')
    writer.writerow(['Name', 'Description'])
    writer.writerow(['Product A', 'A "premium" item'])
    writer.writerow(['Product B', 'Contains; semicolon'])

# Reading with custom settings
with open('custom.csv', 'r') as file:
    reader = csv.reader(file, delimiter=';', quotechar='"')
    for row in reader:
        print(row)

print("Custom delimiter CSV processing completed")

Large File Processing

from collections import defaultdict

# Processing large CSV files efficiently
def process_large_csv(filename):
    total_rows = 0
    grade_counts = defaultdict(int)
    
    with open(filename, 'r') as file:
        reader = csv.DictReader(file)
        for row in reader:
            total_rows += 1
            grade_counts[row['grade']] += 1
            
            # Process in chunks
            if total_rows % 1000 == 0:
                print(f"Processed {total_rows} rows")
    
    return total_rows, dict(grade_counts)

# Example usage (will work when file exists)
# total, counts = process_large_csv('large_file.csv')
print("Large file processing function defined")

Error Handling and Validation

# Safe CSV reading with error handling
def safe_csv_read(filename):
    try:
        with open(filename, 'r') as file:
            # Detect dialect
            sample = file.read(1024)
            file.seek(0)
            dialect = csv.Sniffer().sniff(sample)
            
            reader = csv.reader(file, dialect)
            data = list(reader)
            return data
    except FileNotFoundError:
        print(f"File {filename} not found")
        return []
    except csv.Error as e:
        print(f"CSV error: {e}")
        return []

# Test the function
data = safe_csv_read('data.csv')
print(f"Read {len(data)} rows")

Format Conversion

# Converting between CSV and other formats
def csv_to_dict_list(filename):
    """Convert CSV to list of dictionaries"""
    with open(filename, 'r') as file:
        reader = csv.DictReader(file)
        return list(reader)

def dict_list_to_csv(data, filename):
    """Convert list of dictionaries to CSV"""
    if not data:
        return
    
    with open(filename, 'w', newline='') as file:
        fieldnames = data[0].keys()
        writer = csv.DictWriter(file, fieldnames=fieldnames)
        writer.writeheader()
        writer.writerows(data)

# Example usage (when files exist)
# students = csv_to_dict_list('students.csv')
# dict_list_to_csv(students, 'backup.csv')
print("CSV conversion functions defined")

📋 CSV Parameters Reference Table

ParameterPurposeCommon Values
delimiterField separator',', ';', '\t'
quotecharQuote character'"', "'"
quotingQuote behaviorcsv.QUOTE_MINIMAL, csv.QUOTE_ALL
lineterminatorLine ending'\n', '\r\n'
skipinitialspaceSkip spaces after delimiterTrue, False

🎯 Key Takeaways

Was this helpful?

😔Poor
🙁Fair
😊Good
😄Great
🤩Excellent