11
Lesson 11
Working with CSV files
Objective
By the end of this lesson, students will be able to read from and write to CSV files using Python’s csv module. They will learn how to handle structured data in CSV format and apply basic techniques for data manipulation within Python.
1. Introduction to CSV files:
CSV (Comma-Separated Values) files are widely used for storing tabular data. Each line in a CSV file typically represents a row in the table, with each value separated by a comma. CSV files are often used in data exchange, making them a practical format for data analysis and storage.
2. Using the csv Module for reading CSV files:
Python’s csv module provides an efficient way to read CSV files using the csv.reader() function.
By the end of this lesson, students will be able to read from and write to CSV files using Python’s csv module. They will learn how to handle structured data in CSV format and apply basic techniques for data manipulation within Python.
1. Introduction to CSV files:
CSV (Comma-Separated Values) files are widely used for storing tabular data. Each line in a CSV file typically represents a row in the table, with each value separated by a comma. CSV files are often used in data exchange, making them a practical format for data analysis and storage.
2. Using the csv Module for reading CSV files:
Python’s csv module provides an efficient way to read CSV files using the csv.reader() function.
import csv with open("data.csv", "r") as file: reader = csv.reader(file) for row in reader: print(row) # Each row is a list of values
In this example, each row from data.csv is read as a list of values, making it easy to manipulate individual fields.
3. Writing data to CSV files:
The csv.writer() function allows you to write data to a CSV file, where each row is passed as a list.
import csv with open("output.csv", "w", newline="") as file: writer = csv.writer(file) writer.writerow(["Name", "Age", "City"]) writer.writerow(["Alice", 30, "New York"])
This example creates a CSV file with a header row and a single data row. Using newline="" is essential for preventing extra blank lines in the file on some systems.
4. Working with headers in CSV files:
When reading CSV files with headers, the csv.DictReader class is useful. It reads each row as a dictionary, with keys corresponding to the header row.
with open("data_with_header.csv", "r") as file: reader = csv.DictReader(file) for row in reader: print(row["Name"], row["Age"]) # Access columns by header name
The DictReader is helpful for making code readable and accessing data by field names.
5. Writing data with headers using DictWriter:
The csv.DictWriter class allows writing dictionaries to CSV files, which can simplify data management and make it easier to add or remove fields as needed.
with open("output_with_header.csv", "w", newline="") as file: fieldnames = ["Name", "Age", "City"] writer = csv.DictWriter(file, fieldnames=fieldnames) writer.writeheader() # Write the header row writer.writerow({"Name": "Alice", "Age": 30, "City": "New York"})
This example writes a header and a data row, using fieldnames for dictionary keys.
6. Practical use cases for CSV files:
CSV files are commonly used for:
- Data analysis and storage.
- Importing/exporting data between applications.
- Handling structured datasets in scripts and automated workflows.
7. Important considerations for working with CSV Files:
- Delimiter: By default, CSV files use a comma, but csv.reader() and csv.writer() allow for custom delimiters (e.g., delimiter=';').
- Quote characters: Handle data that contains commas or special characters using the quotechar argument.
- File encodings: Specify encoding (e.g., encoding="utf-8") to avoid issues with non-ASCII characters.
8. Practical examples and exercises:
Exercise 1: Reading and printing CSV data
1. Write a program that reads a CSV file and prints each row.
Exercise 2: Writing data to a new CSV file
1. Create a list of dictionaries representing users (with fields like "Name," "Age," "City").
2. Write this data to a new CSV file using DictWriter.
Exercise 3: Filtering and saving Data
1. Read data from a CSV file, filter rows based on a condition (e.g., Age > 25).
2. Write the filtered data to a new CSV file.
Exercise 4: Appending Data to a CSV File
1. Open a CSV file in append mode ("a").
2. Write additional rows of data to the file without overwriting existing content.
Exercise 5: Using different delimiters
1. Create and read a CSV file using a custom delimiter (e.g., '|').
2. Practice reading and writing using csv.reader() and csv.writer() with this delimiter.
Conclusion
In this lesson, students learned to handle CSV files in Python, including reading, writing, and using headers. They explored the csv module’s various functions, allowing them to work efficiently with structured data. These skills are essential for tasks in data processing, analysis, and automation in real-world applications.