In this blog series, we'll explore how to handle files in Python, starting from the basics and gradually progressing to more advanced techniques.
By the end of this series, you'll have a strong understanding of file operations in Python, enabling you to efficiently manage and manipulate data stored in files.
The series will consist of five posts, each building on the knowledge from the previous one:
- Introduction to File Handling in Python: Reading and Writing Files
- Working with Different File Modes and File Types
- Handling Large Files and File Operations in Python
- Using Context Managers and Exception Handling for Robust File Operations
- (This Post) Advanced File Operations: Working with CSV, JSON, and Binary Files
So far in this series on file handling in Python, weβve covered the basics of reading and writing files, handling large files, and ensuring robust file operations with context managers and exception handling.
This final post will explore advanced file operations, focusing on common formats like CSV, JSON, and binary files.
These formats are widely used for data exchange and storage, and mastering how to work with them will give you the tools to handle a variety of file types efficiently in Python.
Working with CSV Files
CSV (Comma-Separated Values) files are commonly used to store tabular data such as spreadsheets or databases.
Python makes working with CSV files easy through the built-in csv
module, which provides functionality to both read and write CSV files.
Reading CSV Files
To read a CSV file, we use the csv.reader()
function, which returns a reader object that can be iterated over to access rows in the file.
Example: Reading a CSV File
import csv
# Open the CSV file for reading
with open('data.csv', 'r') as file:
reader = csv.reader(file)
# Iterate through each row in the CSV
for row in reader:
print(row)
In this example, we open the CSV file using a context manager and read it row by row using csv.reader()
.
Each row is returned as a list of values, making it easy to process the data.
Reading CSV Files with Headers
If your CSV file has headers, you can use the csv.DictReader()
function to access each row as a dictionary, where the keys are the column headers.
import csv
# Open the CSV file with headers
with open('data_with_headers.csv', 'r') as file:
reader = csv.DictReader(file)
# Iterate through each row, accessing values by column name
for row in reader:
print(f"Name: {row['Name']}, Age: {row['Age']}")
In this case, the DictReader()
function treats the first row of the CSV as headers, allowing you to reference columns by their header names.
Writing to CSV Files
Writing to a CSV file is just as straightforward.
The csv.writer()
function creates a writer object that allows you to write rows to a file.
Example: Writing to a CSV File
import csv
# Data to write to CSV
data = [
["Name", "Age", "Occupation"],
["Alice", 30, "Engineer"],
["Bob", 25, "Designer"],
["Charlie", 35, "Teacher"]
]
# Open a CSV file for writing
with open('output.csv', 'w', newline='') as file:
writer = csv.writer(file)
# Write each row of data
writer.writerows(data)
In this example, we write multiple rows of data to a CSV file using writer.writerows()
.
Each sublist in the data
list represents a row in the CSV.
Get "Python's Magic Methods - Beyond __init__ and __str__"
Magic methods are not just syntactic sugar, they're powerful tools that can significantly improve the functionality and performance of your code. With this book, you'll learn how to use these tools correctly and unlock the full potential of Python.
Working with JSON Files
JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write, and easy for machines to parse and generate.
Pythonβs json
module provides functions to read from and write to JSON files, making it a common format for working with structured data.
Reading JSON Files
Reading a JSON file in Python is as simple as using the json.load()
function, which converts the JSON data into Python data structures (e.g., dictionaries or lists).
Example: Reading a JSON File
import json
# Open and read the JSON file
with open('data.json', 'r') as file:
data = json.load(file)
# Accessing JSON data as a Python dictionary
print(data['name'])
print(data['age'])
In this example, we load the JSON data into a Python dictionary and access individual elements by their keys.
Writing to JSON Files
To write Python data to a JSON file, you can use the json.dump()
function.
This function serializes Python objects into a JSON-formatted string and writes it to a file.
Example: Writing to a JSON File
import json
# Data to write to JSON
data = {
"name": "Alice",
"age": 30,
"occupation": "Engineer"
}
# Open a JSON file for writing
with open('output.json', 'w') as file:
json.dump(data, file, indent=4)
In this example, we write a Python dictionary to a JSON file.
The indent=4
argument ensures that the JSON output is formatted nicely with indentation for better readability.
Working with Binary Files
Binary files store data in binary format, and are used for non-text data such as images, audio, video, and serialized data.
Unlike text files, you need to open binary files in binary mode ('rb'
, 'wb'
) to read or write them.
Reading Binary Files
To read a binary file, you can open it in 'rb'
mode and read the data as bytes.
Example: Reading a Binary File
This article is for paid members only
To continue reading this article, upgrade your account to get full access.
Subscribe NowAlready have an account? Sign In