Serialization in Python
Python : Serialization
What is Serialization in Python?
Serialization in Python is the process of converting data structures or objects into a stream of bytes, which can be stored in a file or transmitted over a network. This process is useful when you need to save the state of an object or transfer data between different systems.
- Python provides several built-in modules for serialization, including
pickle
,json
, andmsgpack
. Each of these modules has its own strengths and weaknesses, and the choice of module depends on the specific requirements of your application.
In this tutorial, we will explore the basics of serialization in Python using the pickle
and json
modules.
Pickle
The pickle
module in Python provides a way to serialize and deserialize Python objects. It can convert Python objects into a stream of bytes and vice versa.
The pickle
module is capable of handling a wide range of data types, including custom classes and functions.
Serialization with Pickle in Python
To serialize a Python object using pickle
, you can use the dump()
function to write the object to a file or a byte stream.
import pickle
data = {'name': 'John', 'age': 30, 'city': 'New York'}
with open('data.pickle', 'wb') as f:
pickle.dump(data, f)
In above example, we have a dictionary object data that contains some data
. We then open a file data.pickle
in binary write mode ('wb'
) and use the pickle.dump()
function to write the data to the file.
Deserialization with Pickle in Python
To deserialize a pickle
object, you can use the load()
function to read the object from a file or a byte stream.
import pickle
with open('data.pickle', 'rb') as f:
data = pickle.load(f)
print(data)
In above example, we open the file data.pickle
in binary read mode ('rb'
) and use the pickle.load()
function to read the data from the file. The data is then printed to the console.
JSON module in Python
- JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate.
- The
json
module in Python provides a way to encode and decodeJSON
data.
Serialization with JSON
To serialize a Python object using json, you can use the dump()
function to write the object to a file or a string.
import json
data = {'name': 'John', 'age': 30, 'city': 'New York'}
with open('data.json', 'w') as f:
json.dump(data, f)
In this example, we have a dictionary object data that contains some data. We then open a file data.json
in write mode ('w'
) and use the json.dump()
function to write the data to the file.
Deserialization with JSON
To deserialize a JSON
object, you can use the load()
function to read the object from a file or a string.
import json
with open('data.json', 'r') as f:
data = json.load(f)
print(data)
In this example, we open the file data.json in read mode ('r'
) and use the json.load()
function to read the data from the file. The data is then printed to the console.
Previous Article
Next Article
Python Tutorials
- Hello World
- Variables and Types
- Lists
- Tuple
- Basic Operators
- Strings
- Conditions
- Loops
- Functions
- Classes and Objects
- Dictionaries
- Map
- Filter
- Reduce
- Sets
- Decorators
- Generators
- Modules and Packages
- Numpy Arrays
- Pandas Basics
- List Comprehensions
- Lambda functions
- Multiple Function Arguments
- Partial functions
- Regular Expressions
- Exception Handling
- Serialization
- Code Introspection