Last modified: Jan 27, 2026 By Alexander Williams
Python Write Dict to File: JSON, Pickle, CSV
You need to save your Python dictionary data. Writing it to a file is the solution. This guide shows you how.
You will learn three main methods. Use JSON for readability and sharing. Use Pickle for Python objects. Use CSV for spreadsheet data.
Each method has a specific use case. We will cover them all with examples.
Why Write a Dictionary to a File?
Python dictionaries store data in memory. When your program stops, the data is lost. Writing to a file saves it permanently.
This is called data persistence. It lets you save user settings, program state, or results. You can load the data later.
Files also let you share data between programs. Other languages can read standard formats like JSON.
Method 1: Using the JSON Module
The JSON module is the most common choice. JSON is a universal text format. It is human-readable and language-independent.
Use json.dump() to write a dict to a file. Use json.dumps() to get a JSON string. Let's see an example.
import json
# Create a sample dictionary
user_data = {
"name": "Alice",
"age": 30,
"city": "London",
"skills": ["Python", "Data Analysis"]
}
# Write the dictionary to a JSON file
with open('user_data.json', 'w') as json_file:
json.dump(user_data, json_file, indent=4)
print("Dictionary written to 'user_data.json'")
Dictionary written to 'user_data.json'
The indent=4 argument makes the file readable. The with statement handles file closing automatically.
You can also create a dictionary using a Python dict literal for quick initialization, as shown above.
Here is the content of the created file.
{
"name": "Alice",
"age": 30,
"city": "London",
"skills": [
"Python",
"Data Analysis"
]
}
Method 2: Using the Pickle Module
Pickle is a Python-specific format. It can serialize almost any Python object. This includes complex nested dictionaries.
Warning: Only unpickle data from trusted sources. It can execute arbitrary code.
Use pickle.dump() to write. The file will be in a binary format.
import pickle
# A dictionary with mixed data types
config = {
"threshold": 0.75,
"model_path": "/models/v1.pkl",
"active": True,
"log_history": [("2023-10-01", "success"), ("2023-10-02", "error")]
}
# Write the dictionary to a pickle file
with open('config.pickle', 'wb') as pickle_file: # Note 'wb' for write-binary
pickle.dump(config, pickle_file)
print("Dictionary pickled to 'config.pickle'")
To read it back, use pickle.load() with mode 'rb'. Pickle is great for saving program state.
Method 3: Using the CSV Module
Use CSV when your dictionary represents tabular data. It works well with spreadsheet software.
You typically write a list of dictionaries. Each dict is a row. Keys become column headers.
Use csv.DictWriter for this task.
import csv
# A list of dictionaries (like rows in a table)
employees = [
{"id": 101, "name": "John Doe", "department": "Engineering"},
{"id": 102, "name": "Jane Smith", "department": "Marketing"},
{"id": 103, "name": "Bob Wilson", "department": "Sales"}
]
# Write to a CSV file
with open('employees.csv', 'w', newline='') as csv_file:
# Define the column headers from the dictionary keys
fieldnames = ['id', 'name', 'department']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader() # Write the header row
writer.writerows(employees) # Write all rows
print("List of dictionaries written to 'employees.csv'")
List of dictionaries written to 'employees.csv'
The resulting CSV file is simple and portable.
id,name,department
101,John Doe,Engineering
102,Jane Smith,Marketing
103,Bob Wilson,Sales
Choosing the Right Method
How do you pick the right tool? It depends on your goal.
Use JSON (json module) when you need a human-readable, standard format. It's perfect for config files or web APIs. Before writing, you might want to check if the Python dictionary is empty to avoid writing useless files.
Use Pickle (pickle module) when you need to save complex Python objects exactly. Use it for machine learning models or session data. It is Python-only.
Use CSV (csv module) when your data is table-like. Use it for reports or data exchange with Excel. It works with lists of similar dictionaries.
Advanced Tips and Common Issues
Sometimes your dictionary contains non-serializable objects. JSON cannot handle Python-specific types like sets or datetime.
You must convert them first. Define a custom encoder or use default string conversion.
import json
from datetime import datetime
event = {
"title": "Project Meeting",
"time": datetime.now(), # datetime object is not JSON-serializable
"participants": {"Alice", "Bob"} # set is not JSON-serializable
}
# Simple solution: convert to string in the dump call
with open('event.json', 'w') as f:
json.dump(event, f, default=str) # 'default=str' converts non-serializable objects to string
print("Used default=str to handle non-JSON types.")
For writing, you often need to merge Python dictionaries from different sources into one final structure before saving.
Always handle file operations with try-except blocks. This prevents crashes from permission errors or full disks.
Conclusion
Writing a Python dictionary to a file is a core skill. You learned three key methods.
Use JSON for general-purpose, readable storage. Use Pickle for Python-specific object persistence. Use CSV for tabular data.
Remember to choose the format based on who needs to read the data. Always handle files safely with the with statement.
Now you can save, share, and persist your dictionary data with confidence.