In Python, the built-in csv module provides robust functionality for handling comma-separated value files. This guide explores various techniques for reading from and writing to CSV files efficiently.
Basic CSV Reading Operations
import csv
def process_csv_document(file_location):
"""
Reads a CSV file line by line and prints each row.
"""
with open(file_location, mode='r', newline='', encoding='utf-8') as data_file:
csv_parser = csv.reader(data_file)
for current_row in csv_parser:
print(current_row)
# Implementation example
data_source = 'sample_data.csv'
process_csv_document(data_source)
Writing Data to CSV Files
import csv
def save_to_csv(destination, records):
"""
Writes a list of lists to a CSV file.
"""
with open(destination, mode='w', newline='', encoding='utf-8') as output_file:
csv_writer = csv.writer(output_file)
csv_writer.writerows(records)
# Example usage
output_path = 'generated_data.csv'
dataset = [
['Employee', 'Department', 'Salary'],
['John Smith', 'Engineering', '90000'],
['Sarah Johnson', 'Marketing', '75000'],
['Michael Chen', 'Sales', '80000']
]
save_to_csv(output_path, dataset)
Dictionary-Based CSV Processing
For more structured data handling, dictionary-based methods are often more intuitive:
import csv
def read_csv_as_dictionary(file_path):
"""
Reads CSV file using field names as keys for each row.
"""
with open(file_path, mode='r', newline='', encoding='utf-8') as input_file:
dict_reader = csv.DictReader(input_file)
for entry in dict_reader:
print(entry)
# Example implementation
source_file = 'employee_records.csv'
read_csv_as_dictionary(source_file)
import csv
def export_from_dict(destination, headers, content):
"""
Writes data from a list of dictionaries to CSV.
"""
with open(destination, mode='w', newline='', encoding='utf-8') as output_file:
writer = csv.DictWriter(output_file, fieldnames=headers)
writer.writeheader()
writer.writerows(content)
# Usage example
output_file = 'employee_report.csv'
column_names = ['Employee', 'Department', 'Salary']
employee_data = [
{'Employee': 'John Smith', 'Department': 'Engineering', 'Salary': '90000'},
{'Employee': 'Sarah Johnson', 'Department': 'Marketing', 'Salary': '75000'},
{'Employee': 'Michael Chen', 'Department': 'Sales', 'Salary': '80000'}
]
export_from_dict(output_file, column_names, employee_data)
Memory-Efficient Large File Processing
When dealing with substantial CSV files, memory optimization becomes crucial:
import csv
def process_large_csv(file_path):
"""
Processes large CSV files row by row to minimize memory usage.
"""
with open(file_path, mode='r', newline='', encoding='utf-8') as large_file:
csv_reader = csv.reader(large_file)
for row in csv_reader:
# Process each row individually
process_row(row) # Custom processing function
# Example implementation
massive_dataset = 'big_data.csv'
process_large_csv(massive_dataset)
Handling Complex CSV Formats
CSV files with custom deilmiters or special characters require specific handling:
import csv
def parse_special_format_csv(file_path):
"""
Reads CSV files with non-standard delimiters or quoting.
"""
with open(file_path, mode='r', newline='', encoding='utf-8') as special_file:
# Using semicolon delimiter and double quotes for text fields
custom_reader = csv.reader(special_file, delimiter=';', quotechar='"')
for record in custom_reader:
print(record)
# Example usage
custom_csv = 'international_data.csv'
parse_special_format_csv(custom_csv)
These techniques provide a comprehensive toolkit for CSV file manipualtion in Python, adaptable to various data processing scenarios from simple flat files to complex data structures with custom formatting requirements.