Working with files
Read, process, and write data files
In this tutorial, we'll learn how to read, process, and write data files, a fundamental skill for almost any programming task.
Files in AI projects
Data is everywhere in AI. Your models learn from data, and they produce data as predictions. This data is often stored in files.
Understanding how to work with files is a core skill. It lets you feed data into your programs and save your results. You can read data from a CSV, process it, and then save the output to a new file or a different format.
Common libraries for files
Python has built-in ways to handle basic text files. But for AI work, you'll often need more powerful tools.
pandas: The go-to library for structured data like tables. It makes reading and writing CSVs, Excel files, and more very easy.json: For working with JSON data, which is common for web data and APIs.csv: Python's built-in module for CSV files, useful for simple cases.
Working with our sales data
Let's imagine we have a simple sales dataset. It contains product name, quantity, and price. We'll use this data to explore different file operations.
Product,Quantity,Price
Laptop,1,1200.00
Mouse,2,25.50
Keyboard,1,75.00This is a CSV (Comma Separated Values) format. Each line is a record, and commas separate the fields.
File format comparison
Different file formats have different strengths. Choosing the right one depends on your data and what you plan to do with it.
- CSV (Comma Separated Values): Simple, human-readable, good for tabular data. Not ideal for complex structures or large datasets.
- JSON (JavaScript Object Notation): Lightweight, human-readable, excellent for nested data and web communication. Less efficient for purely tabular data than CSV.
- Excel (XLSX): Good for spreadsheets, can contain multiple sheets. Requires specific libraries to handle.
- Parquet/ORC: Binary, columnar storage formats. Highly efficient for large datasets and analytical queries. Not human-readable.
Loading different file types
Let's see how to load our sales data using pandas for CSV and Python's json module.
First, create a sales.csv file with the content from above.
echo "Product,Quantity,Price
Laptop,1,1200.00
Mouse,2,25.50
Keyboard,1,75.00" > sales.csvUsing pandas to read CSV:
import pandas as pd
df = pd.read_csv('sales.csv')
print(df)Now, let's represent the same data in JSON format in a file called sales.json.
echo '[{"Product": "Laptop", "Quantity": 1, "Price": 1200.00},
{"Product": "Mouse", "Quantity": 2, "Price": 25.50},
{"Product": "Keyboard", "Quantity": 1, "Price": 75.00}]' > sales.jsonUsing Python's json module to read JSON:
import json
with open('sales.json', 'r') as f:
data = json.load(f)
print(data)Learn more
File handling is a vast topic. With these basics, you're off to a great start. Explore the documentation for pandas and Python's built-in file operations to deepen your knowledge.
Next, you might want to learn about error handling (try-except blocks) for when files aren't found or are corrupted. Also, explore writing data back to files.