Course contentsShow
← Python
Lesson 738 of 1,06011. Standard Library EssentialsFree lesson

Choosing Between Text and Binary Formats

Evaluate the trade-offs between JSON, CSV, and pickle formats to select the most efficient serialization strategy for specific project requirements.

In this lesson, you will learn how to evaluate the trade-offs between JSON, CSV, and pickle to choose the most efficient serialization strategy for your application.

When storing data, your choice hinges on the balance between human readability, portability, and performance.

Text-Based Formats: JSON and CSV

Text formats represent data as plain, readable characters.

  • JSON is the gold standard for hierarchical data (dictionaries and lists). It is highly portable across different programming languages but can become verbose and slow for very large datasets.
  • CSV is ideal for flat, tabular data. It is extremely space-efficient for simple structures and works seamlessly with spreadsheet software or data analysis tools like pandas.

Use these when you need to inspect files manually or share data with systems written in other languages (e.g., JavaScript or C#).

Binary Formats: Pickle

Binary formats store objects as a byte stream. The pickle module allows you to serialize almost any Python object—including custom class instances—directly to disk.

Because it maps Python objects directly to binary, it is often faster and more compact than converting complex objects into a text-based format like JSON. However, this comes with a strict limitation: the resulting file is Python-specific. You cannot easily read a pickle file in a different programming language. Furthermore, you must never unpickle data from an untrusted source, as it can execute arbitrary code.

Decision Guide

Consider your project requirements:

  • Interoperability: If your data must be read by non-Python tools, use JSON or CSV.
  • Complexity: If you need to save complex custom class instances without writing custom serialization logic, pickle is the most convenient tool.
  • Security: If the file will be downloaded or modified by others, avoid pickle to prevent potential security vulnerabilities.
# Use CSV for simple, flat tabular data
import csv
with open('data.csv', 'w') as f:
    writer = csv.writer(f)
    writer.writerow(['ID', 'Name'])

# Use JSON for nested, cross-language configurations
import json
data = {"user": "Alice", "preferences": {"theme": "dark"}}
json_str = json.dumps(data)

# Use pickle ONLY for internal, trusted Python-to-Python storage
import pickle
with open('state.pkl', 'wb') as f:
    pickle.dump(my_complex_object, f)

Key Takeaway: Choose text-based formats (JSON/CSV) for interoperability and security, and reserve binary formats (pickle) for internal, high-performance serialization of complex Python objects.