# Step 1: Create sample CSV
import csv
= [['name','email','age'],
data 'Alice','[email protected]',28],
['Bob','[email protected]',35]]
[with open(r'C:\Users\steve\Documents\GitHub\steveondata\posts\2025-10-01\sample.csv', 'w') as f:
= csv.writer(f)
writer
writer.writerows(data)
# Step 2: Convert to JSON
import json
with open(r'C:\Users\steve\Documents\GitHub\steveondata\posts\2025-10-01\sample.csv') as f:
= csv.DictReader(f)
reader = list(reader)
rows with open(r'C:\Users\steve\Documents\GitHub\steveondata\posts\2025-10-01\sample.json', 'w') as f:
=2)
json.dump(rows, f, indent
# Step 3: Add new field
with open(r'C:\Users\steve\Documents\GitHub\steveondata\posts\2025-10-01\sample.json') as f:
= json.load(f)
data for item in data:
'status'] = 'active'
item[with open(r'C:\Users\steve\Documents\GitHub\steveondata\posts\2025-10-01\updated.json', 'w') as f:
=2)
json.dump(data, f, indent
# Step 4: Convert back to CSV
with open(r'C:\Users\steve\Documents\GitHub\steveondata\posts\2025-10-01\updated.json') as f:
= json.load(f)
data with open(r'C:\Users\steve\Documents\GitHub\steveondata\posts\2025-10-01\final.csv', 'w') as f:
= csv.DictWriter(f, fieldnames=data[0].keys())
writer
writer.writeheader() writer.writerows(data)
Authors Note: I am learning as I write this series, so I might make mistakes or do things that are not as efficient as they could be.
Introduction
Python offers straightforward ways to handle CSV and JSON files, two common formats for storing and exchanging data. This guide shows how to read, write, and convert between these formats using Python’s built-in tools and popular libraries.
Reading and Writing CSV Files
Python’s csv
module provides simple methods for CSV operations:
import csv
# Reading a CSV file
with open('data.csv') as file:
= csv.reader(file)
reader for row in reader:
print(row)
# Writing to CSV
with open('output.csv', 'w') as file:
= csv.writer(file)
writer 'Name', 'Age'])
writer.writerow(['Alice', 30]) writer.writerow([
For more advanced data handling, the pandas library simplifies CSV operations:
import pandas as pd
# Read CSV into a DataFrame
= pd.read_csv('data.csv')
data
# Write DataFrame to CSV
'output.csv', index=False) data.to_csv(
Working with JSON Data
Python’s json
module makes JSON operations simple:
import json
# Reading JSON
with open('data.json') as file:
= json.load(file)
data
# Writing JSON
with open('output.json', 'w') as file:
file, indent=4) json.dump(data,
Converting Between CSV and JSON
Converting between formats is straightforward:
CSV to JSON:
import csv, json
= []
csv_data with open('data.csv') as file:
= csv.DictReader(file)
reader for row in reader:
csv_data.append(row)
with open('output.json', 'w') as file:
file, indent=4) json.dump(csv_data,
JSON to CSV:
import csv, json
with open('data.json') as file:
= json.load(file)
json_data
with open('output.csv', 'w') as file:
= csv.DictWriter(file, fieldnames=json_data[0].keys())
writer
writer.writeheader() writer.writerows(json_data)
Your Turn!
- Create a CSV file with sample data (name, email, age)
- Write a Python script to convert it to JSON
- Modify the JSON data by adding a new field
- Convert the modified JSON back to CSV
Solution Example
Key Points
- The
csv
module handles basic CSV operations - pandas provides advanced CSV capabilities
- JSON works naturally with Python dictionaries
- Conversion between formats is simple with the right tools
References
- Python csv module documentation - Official documentation for Python’s built-in CSV handling capabilities
- Python json module documentation - Official guide for working with JSON data in Python
- pandas read_csv documentation - Complete reference for reading CSV files into DataFrames
- pandas to_csv documentation - Detailed guide for writing DataFrames to CSV files
Happy Coding! 🚀
You can connect with me at any one of the below:
Telegram Channel here: https://t.me/steveondata
LinkedIn Network here: https://www.linkedin.com/in/spsanderson/
Mastadon Social here: https://mstdn.social/@stevensanderson
RStats Network here: https://rstats.me/@spsanderson
GitHub Network here: https://github.com/spsanderson
Bluesky Network here: https://bsky.app/profile/spsanderson.com
My Book: Extending Excel with Python and R here: https://packt.link/oTyZJ
You.com Referral Link: https://you.com/join/EHSLDTL6