# Step 1: Create sample CSV
import csv
data = [['name','email','age'],
['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:
writer = csv.writer(f)
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:
reader = csv.DictReader(f)
rows = list(reader)
with open(r'C:\Users\steve\Documents\GitHub\steveondata\posts\2025-10-01\sample.json', 'w') as f:
json.dump(rows, f, indent=2)
# Step 3: Add new field
with open(r'C:\Users\steve\Documents\GitHub\steveondata\posts\2025-10-01\sample.json') as f:
data = json.load(f)
for item in data:
item['status'] = 'active'
with open(r'C:\Users\steve\Documents\GitHub\steveondata\posts\2025-10-01\updated.json', 'w') as f:
json.dump(data, f, indent=2)
# Step 4: Convert back to CSV
with open(r'C:\Users\steve\Documents\GitHub\steveondata\posts\2025-10-01\updated.json') as f:
data = json.load(f)
with open(r'C:\Users\steve\Documents\GitHub\steveondata\posts\2025-10-01\final.csv', 'w') as f:
writer = csv.DictWriter(f, fieldnames=data[0].keys())
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:
reader = csv.reader(file)
for row in reader:
print(row)
# Writing to CSV
with open('output.csv', 'w') as file:
writer = csv.writer(file)
writer.writerow(['Name', 'Age'])
writer.writerow(['Alice', 30])For more advanced data handling, the pandas library simplifies CSV operations:
import pandas as pd
# Read CSV into a DataFrame
data = pd.read_csv('data.csv')
# Write DataFrame to CSV
data.to_csv('output.csv', index=False)Working with JSON Data
Python’s json module makes JSON operations simple:
import json
# Reading JSON
with open('data.json') as file:
data = json.load(file)
# Writing JSON
with open('output.json', 'w') as file:
json.dump(data, file, indent=4)Converting Between CSV and JSON
Converting between formats is straightforward:
CSV to JSON:
import csv, json
csv_data = []
with open('data.csv') as file:
reader = csv.DictReader(file)
for row in reader:
csv_data.append(row)
with open('output.json', 'w') as file:
json.dump(csv_data, file, indent=4)JSON to CSV:
import csv, json
with open('data.json') as file:
json_data = json.load(file)
with open('output.csv', 'w') as file:
writer = csv.DictWriter(file, fieldnames=json_data[0].keys())
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
csvmodule 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