Python CSV Files: Handling CSV files Using the 'csv' module
Introduction to Python CSV Files Handling
CSV (Comma-Separated Values) files are a common format for storing tabular data. Python's built-in 'csv' module provides tools to read from and write to CSV files efficiently. In this tutorial we covered reading and writing CSV files, working with headers, custom delimiters, and quoting. With these examples, you should be well-equipped to manage CSV files in your Python projects.
Reading CSV Files:
To read a CSV file in Python, you can use the csv.reader object. This allows you to iterate over the rows of the file, where each row is a list of strings.
Example 1: Reading a Simple CSV File
This example shows how to read the contents of a CSV file and print each row as a list of strings.
Code:
import csv
# Open the CSV file in read mode
with open('test.csv', 'r') as file:
# Create a CSV reader object
reader = csv.reader(file)
# Iterate over the rows in the CSV file
for row in reader:
print(row) # Print each row as a list of strings
Output:
['col1', 'col2', 'col3'] ['20', '40', '60'] ['30', '50', '80'] ['40', '60', '100']
Explanation:
The 'csv.reader(file)' creates a reader object that iterates over lines in the given CSV file. Each row from the file is read as a list of strings, which are printed out one by one. The with statement ensures the file is properly closed after reading.
Writing to a CSV File:
To write data to a CSV file, the 'csv.writer' object is used. This object provides methods like 'writerow()' to write a single row and 'writerows()' to write multiple rows.
Example 2: Writing Data to a CSV File
This example demonstrates how to write a list of lists to a CSV file, where each inner list represents a row.
Code:
import csv
# Data to write to the CSV file
data = [
['Name', 'Age', 'City'],
['Leatrice', '30', 'New York'],
['Khufu', '25', 'Los Angeles'],
['Monique', '35', 'Chicago']
]
# Open the CSV file in write mode
with open('output.csv', 'w', newline='') as file:
# Create a CSV writer object
writer = csv.writer(file)
# Write the rows of data to the CSV file
writer.writerows(data)
Explanation:
The 'csv.writer(file)' creates a writer object. The 'writerows(data)' method writes each list in 'data' as a row in the CSV file. The 'newline=''' parameter in 'open()' is used to prevent extra blank lines in the output file on some systems (e.g., Windows).
Reading CSV Files with a Header:
CSV files often contain a header row that labels the columns. The 'csv.DictReader' object reads CSV files into dictionaries, where the keys are the column headers.
Example 3: Reading a CSV File with a Header
This example shows how to read a CSV file that contains a header row, returning each row as a dictionary.
Code:
import csv
# Open the CSV file in read mode
with open('test.csv', 'r') as file:
# Create a DictReader object
reader = csv.DictReader(file)
# Iterate over each row as a dictionary
for row in reader:
print(row) # Print each row as a dictionary
Output:
{'col1': '20', 'col2': '40', 'col3': '60'} {'col1': '30', 'col2': '50', 'col3': '80'} {'col1': '40', 'col2': '60', 'col3': '100'}
Explanation:
The 'csv.DictReader(file)' reads each row in the CSV file and maps it to a dictionary using the header row as the keys. This approach makes it easy to access columns by their names, rather than by index.
Writing to a CSV File with a Header:
Similar to 'DictReader', the 'csv.DictWriter' class is used to write dictionaries to a CSV file. You need to specify the fieldnames (i.e., column headers) when creating the 'DictWriter' object.
Example 4: Writing Data with a Header to a CSV File
This example demonstrates how to write a list of dictionaries to a CSV file, including a header row.
Code:
import csv
# Data to write to the CSV file
data = [
{'Name': 'Katerina Ivan', 'Age': '28', 'City': 'New York'},
{'Name': 'Sulo Ruprecht', 'Age': '25', 'City': 'Los Angeles'},
{'Name': 'Enyinnaya Aarti', 'Age': '30', 'City': 'Chicago'}
]
# Open the CSV file in write mode
with open('person.csv', 'w', newline='') as file:
# Define the fieldnames (column headers)
fieldnames = ['Name', 'Age', 'City']
# Create a DictWriter object
writer = csv.DictWriter(file, fieldnames=fieldnames)
# Write the header row to the CSV file
writer.writeheader()
# Write the data rows to the CSV file
writer.writerows(data)
Explanation:
The 'csv.DictWriter(file, fieldnames=fieldnames)' creates a writer object configured with the specified column headers. The 'writeheader()' method writes the header row, and 'writerows(data)' writes each dictionary in 'data' as a row in the CSV file.
Handling Different Delimiters:
CSV files can use delimiters other than commas, such as tabs ('\t') or semicolons (';'). The 'csv' module allows you to specify a custom delimiter.
Example 5: Reading and Writing CSV Files with a Custom Delimiter
This example shows how to handle CSV files that use a custom delimiter (in this case, a semicolon ';').
Code:
import csv
# Data to write to the CSV file
data = [
['Name', 'Age', 'City'],
['Dimitri Valtteri', '30', 'New York'],
['Wilfrith Heilyn', '25', 'Los Angeles'],
['Margaid Toma', '35', 'Chicago']
]
# Write data to a semicolon-delimited CSV file
with open('semicolon_delimited.csv', 'w', newline='') as file:
writer = csv.writer(file, delimiter=';')
writer.writerows(data)
# Read the semicolon-delimited CSV file
with open('semicolon_delimited.csv', 'r') as file:
reader = csv.reader(file, delimiter=';')
for row in reader:
print(row)
Output:
['Name', 'Age', 'City'] ['Dimitri Valtteri', '30', 'New York'] ['Wilfrith Heilyn', '25', 'Los Angeles'] ['Margaid Toma', '35', 'Chicago']
Explanation:
The 'csv.writer()' and 'csv.reader()' objects accept a ‘delimiter’ argument that specifies the character used to separate fields in the file. In this example, semicolons are used as delimiters instead of commas.
Handling Quoting in CSV Files:
CSV files may contain fields that include the delimiter character, line breaks, or quotes. The 'csv' module handles these cases using quoting options.
Example 6: Reading and Writing CSV Files with Quoting
This example illustrates how to handle CSV files with fields that contain quotes, commas, or line breaks, using the ‘quoting’ parameter.
Code:
import csv
# Data to write to the CSV file
data = [
['Name', 'Quote'],
['Boris Marciana', 'Hello, World!'],
['Serdar Gayathri', 'Python is "amazing"!'],
['Vasiliy Brunella', 'Keep calm and code on.']
]
# Write data to a CSV file with quoting
with open('quoted.csv', 'w', newline='') as file:
writer = csv.writer(file, quoting=csv.QUOTE_ALL)
writer.writerows(data)
# Read the CSV file with quoting
with open('quoted.csv', 'r') as file:
reader = csv.reader(file, quoting=csv.QUOTE_ALL)
for row in reader:
print(row)
Output:
['Name', 'Quote'] ['Boris Marciana', 'Hello, World!'] ['Serdar Gayathri', 'Python is "amazing"!'] ['Vasiliy Brunella', 'Keep calm and code on.']
Explanation:
The 'quoting=csv.QUOTE_ALL' option ensures that all fields in the CSV file are quoted, which prevents issues with fields containing special characters like commas or quotes. The reader handles these fields correctly when reading the file back.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://198.211.115.131/python/python-handling-csv-files-with-examples.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics