Hey everyone,
I’m managing a bunch of image data, like filenames, tags, and URLs, and I’ve been told that using CSV files is a simple way to store and handle this info. I want to automate some of this using Python.
Can someone walk me through how to read from and write to a Python CSV file? Bonus points if it relates to image processing or something like Cloudinary!
Thanks in advance!
Absolutely, and great question! CSV (Comma-Separated Values) files are a lightweight and widely-used format for storing structured data, perfect for handling image filenames, tags, upload statuses, and more. They aren’t exclusive to Python, CSV files are used in all sorts of applications.
In Python, the built-in csv module makes it easy to read, write, and manipulate CSV files. Let’s walk through everything step-by-step with examples tailored to media and image workflows.
A CSV file is a plain text file where each line represents a row of data, and each value in a row is separated by a comma:
filename,tag,url
image1.jpg,portrait,https://example.com/image1.jpg
image2.jpg,landscape,https://example.com/image2.jpg
Code language: JavaScript (javascript)
This structure makes CSVs perfect for use with spreadsheets and automation scripts.
Let’s say you have a CSV file named images.csv
:
import csv
with open('images.csv', newline='') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
print(row)
Code language: JavaScript (javascript)
This will output:
['filename', 'tag', 'url']
['image1.jpg', 'portrait', 'https://example.com/image1.jpg']
['image2.jpg', 'landscape', 'https://example.com/image2.jpg']
Code language: CSS (css)
Rather than accessing by index (row[0], row[1]
…), you can use headers with DictReader:
with open('images.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print(f"Uploading {row['filename']} with tag {row['tag']}")
Code language: JavaScript (javascript)
Much cleaner, especially when handling structured image metadata.
To create or overwrite a CSV file:
import csv
with open('output.csv', mode='w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['filename', 'tag', 'url']) # Header
writer.writerow(['image1.jpg', 'portrait', 'https://example.com/image1.jpg'])
writer.writerow(['image2.jpg', 'landscape', 'https://example.com/image2.jpg'])
Code language: PHP (php)
data = [
{'filename': 'image1.jpg', 'tag': 'portrait', 'url': 'https://example.com/image1.jpg'},
{'filename': 'image2.jpg', 'tag': 'landscape', 'url': 'https://example.com/image2.jpg'},
]
with open('output.csv', mode='w', newline='') as csvfile:
fieldnames = ['filename', 'tag', 'url']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for row in data:
writer.writerow(row)
Code language: JavaScript (javascript)
Let’s combine Cloudinary with a CSV read:
import csv
import cloudinary.uploader
with open('images.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
response = cloudinary.uploader.upload(row['filename'], tags=[row['tag']])
print(f"Uploaded {row['filename']} → {response['secure_url']}")
Code language: JavaScript (javascript)
This script uploads each file listed in the CSV and tags it accordingly. It’s ideal for batch uploads and metadata automation.
Task | Tool | Example |
Read CSV | csv.reader | reader = csv.reader(file) |
Read with headers | csv.DictReader | reader = csv.DictReader(file) |
Write CSV | csv.writer | writer.writerow([...]) |
Write with dicts | csv.DictWriter | writer.writerow({key: value, ...}) |
Best for media handling | Use DictReader/DictWriter with filenames, tags, URLs |
- Don’t forget
newline=''
when opening files to avoid extra blank lines in Windows. - Always close files, or use
with open(...)
to do it automatically. - If you need more complex parsing (e.g., semicolons or quoted fields), check the csv module’s delimiter options.
This format is perfect for in-memory manipulation:
with open('images.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
image_data = list(reader)
print(image_data[0]['filename']) # Access like a dictionary
Code language: PHP (php)
To add a new row:
with open('images.csv', mode='a', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['new_image.jpg', 'editorial', 'https://example.com/new_image.jpg'])
Code language: JavaScript (javascript)
Whether you’re batch uploading images, managing tags, or logging metadata, working with Python and CSV files is simple and powerful. Python’s csv
module is beginner-friendly and robust for real-world tasks, including Cloudinary automation.