Introduction
In this lesson, we will delve into handling CSV files with Python's built-in csv module. CSV, or Comma-Separated Values, is a widely used format for storing tabular data. By leveraging the csv module, we can easily read from and write to CSV files, manage various data delimiters, and handle records both as lists and dictionaries. These skills are vital for data manipulation in diverse regional formats, and important for any data-driven Python application.
Reading CSV Files with csv.reader
Basic Usage
To read a CSV file, open the file in read mode and pass it to csv.reader. This returns an iterable of rows, each represented as a list of strings.
import csv
with open('data.csv', newline='', encoding='utf-8') as csvfile:
csvreader = csv.reader(csvfile)
for row in csvreader:
print(row)
Handling Delimiters and Headers
CSV files may use delimiters other than commas, such as semicolons or tabs. Specify the delimiter explicitly to adapt to these variations:
with open('data.csv', newline='', encoding='utf-8') as csvfile:
csvreader = csv.reader(csvfile, delimiter=';')
headers = next(csvreader) # Skip the header row
for row in csvreader:
print(row)
// Output: Lists of CSV data where each item represents a column based on the delimiter
Writing CSV Files with csv.writer
The csv.writer class allows you to write data into a CSV file efficiently, handling various formatting options such as quoting and line terminators:
import csv
with open('output.csv', 'w', newline='', encoding='utf-8') as csvfile:
csvwriter = csv.writer(csvfile, quoting=csv.QUOTE_MINIMAL)
csvwriter.writerow(['Name', 'Age', 'City'])
csvwriter.writerow(['Alice', '30', 'New York'])
// Output: Creates a CSV file with rows written as specified.
Using DictReader and DictWriter
DictReader and DictWriter provide dictionary-like capabilities to CSV data manipulation. These classes allow working with data by column names, enhancing code readability and flexibility.
Reading as Dictionaries
with open('data.csv', newline='', encoding='utf-8') as csvfile:
csvreader = csv.DictReader(csvfile)
for row in csvreader:
print(row['Name'], row['City']) # Access column by header name
Writing with Dictionaries
fieldnames = ['Name', 'Age', 'City']
with open('output.csv', 'w', newline='', encoding='utf-8') as csvfile:
csvwriter = csv.DictWriter(csvfile, fieldnames=fieldnames)
csvwriter.writeheader()
csvwriter.writerow({'Name': 'Bob', 'Age': 25, 'City': 'Los Angeles'})
// Output: Writes CSV file with dictionaries as input maintaining header structure.
Configuring Dialects
Dialects in the csv module allow defining a set of formatting parameters for CSV operation, useful for adhering to specific regional conventions or internal data formats.
csv.register_dialect('myDialect', delimiter='|', quoting=csv.QUOTE_ALL)
with open('data.csv', newline='', encoding='utf-8') as csvfile:
csvreader = csv.reader(csvfile, dialect='myDialect')
for row in csvreader:
print(row)
Handling Encoding, Errors, and Validation
Ensure correct file encoding (e.g., UTF-8) to manage international character sets and prevent data corruption. Employ error handling to catch and handle potential file reading/writing exceptions.
try:
with open('data.csv', newline='', encoding='utf-8') as csvfile:
csvreader = csv.reader(csvfile)
for row in csvreader:
# Validate data fields in row
if valid(row):
process(row)
except FileNotFoundError as e:
print(f'Error: {e}')
Summary / Key Takeaways
In this lesson on working with CSV files using the csv module, you have learned:
- Reading CSV Files: How to read CSV data with different delimiters and skip headers using
csv.reader. - Writing CSV Files: The use of
csv.writerto efficiently write rows, handling quoting and line terminators. - DictReader and DictWriter: Utilization to access CSV data as dictionaries, supporting easy column-based operations.
- Configuring Dialects: Managing different CSV formats and regional conventions using dialects.
- Handling Encoding and Errors: Strategies for encoding considerations and implementing error handling during file operations.
