CSV (Comma-Separated Values) is a widely used file format for storing tabular data, making it an essential skill for any programmer dealing with data manipulation and analysis. In this tutorial, we will explore how to read from and write to CSV files using Python's built-in csv module. We'll cover various scenarios, including handling different delimiters, managing headers, and working with large datasets.
Before diving into the code examples, ensure you have Python installed on your system. The csv module is part of Python's standard library, so no additional installation is required.
To read a CSV file, we can use the csv.reader() function. This function returns an iterator that produces lists corresponding to each row in the CSV file.
import csv
# Open the CSV file for reading
with open('example.csv', mode='r') as file:
# Create a CSV reader object
csv_reader = csv.reader(file)
# Iterate over each row in the CSV file
for row in csv_reader:
print(row)
If your CSV file has headers, you can use csv.DictReader() to read the data into dictionaries. This makes it easier to access columns by their names.
import csv
# Open the CSV file for reading
with open('example.csv', mode='r') as file:
# Create a DictReader object
csv_reader = csv.DictReader(file)
# Iterate over each row in the CSV file
for row in csv_reader:
print(row['Name'], row['Age'])
By default, csv.reader() and csv.DictReader() use a comma as the delimiter. However, you can specify a different delimiter using the delimiter parameter.
import csv
# Open the CSV file for reading with a semicolon delimiter
with open('example.csv', mode='r') as file:
# Create a CSV reader object with a semicolon delimiter
csv_reader = csv.reader(file, delimiter=';')
# Iterate over each row in the CSV file
for row in csv_reader:
print(row)
To write data to a CSV file, use csv.writer(). This function returns a writer object that converts user data into delimited strings on the given file-like object.
import csv
# Data to be written to the CSV file
data = [
['Name', 'Age', 'City'],
['Alice', 30, 'New York'],
['Bob', 25, 'Los Angeles']
]
# Open the CSV file for writing
with open('output.csv', mode='w', newline='') as file:
# Create a CSV writer object
csv_writer = csv.writer(file)
# Write data to the CSV file
for row in data:
csv_writer.writerow(row)
To write data with headers, use csv.DictWriter(). This function allows you to specify the fieldnames and write dictionaries to the CSV file.
import csv
# Data to be written to the CSV file
data = [
{'Name': 'Alice', 'Age': 30, 'City': 'New York'},
{'Name': 'Bob', 'Age': 25, 'City': 'Los Angeles'}
]
# Open the CSV file for writing
with open('output.csv', mode='w', newline='') as file:
# Create a DictWriter object with fieldnames
csv_writer = csv.DictWriter(file, fieldnames=['Name', 'Age', 'City'])
# Write the header row
csv_writer.writeheader()
# Write data rows
for row in data:
csv_writer.writerow(row)
Similar to reading, you can specify a different delimiter when writing using the delimiter parameter.
import csv
# Data to be written to the CSV file
data = [
['Name', 'Age', 'City'],
['Alice', 30, 'New York'],
['Bob', 25, 'Los Angeles']
]
# Open the CSV file for writing with a semicolon delimiter
with open('output.csv', mode='w', newline='') as file:
# Create a CSV writer object with a semicolon delimiter
csv_writer = csv.writer(file, delimiter=';')
# Write data to the CSV file
for row in data:
csv_writer.writerow(row)
with statements when opening files to ensure that files are properly closed after their suite finishes.FileNotFoundError or csv.Error.encoding='utf-8') when opening files.Reading and writing CSV files in Python is straightforward thanks to the built-in csv module. By following best practices and understanding how to handle different scenarios, you can efficiently manage data in CSV format for various applications.
Feel free to experiment with these examples and adapt them to fit your specific needs. Happy coding!