Working with CSV Files and JSON Data in Python

Learn how to read, write, and convert CSV and JSON files in Python with beginner friendly examples. This guide covers practical code snippets, tips for using the csv and json modules, and a hands on activity to help you master data handling in Python.
code
python
Author

Steven P. Sanderson II, MPH

Published

October 1, 2025

Keywords

Programming, csv files, json data, python data handling, read and write csv python, convert csv to json python, beginner python csv tutorial, python json module example, csv vs json in python, pandas read_csv guide, how to manipulate structured data in python, how to convert csv file to json in python, beginner python script for reading csv and writing json, step by step guide to csv and json handling in python, python code example for csv to json conversion, working with structured data formats in python for beginners

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!

  1. Create a CSV file with sample data (name, email, age)
  2. Write a Python script to convert it to JSON
  3. Modify the JSON data by adding a new field
  4. Convert the modified JSON back to CSV
Solution Example
# 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)

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


Happy Coding! 🚀

CSV and JSON with Python

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