Serialize and Deserialize an Open File Object in Python
Last Updated :
22 Mar, 2024
Serialization refers to the process of converting an object into a format that can be easily stored or transmitted, such as a byte stream. Deserialization, on the other hand, involves reconstructing the object from its serialized form. When dealing with file operations, it's common to serialize data before writing it to a file and deserialize it when reading it back. In this article, we'll explore how to serialize and deserialize an open file object in Python.
Serialize and Deserialize an Open File Object in Python
Below are the ways to Serialize and Deserialize an Open File Object in Python:
- Using Pickle
- Using JSON
- Using YAML
Serialize and Deserialize an Open File Object Using Pickle
This Python code serializes a dictionary `data` into a binary file named 'data.pickle' using the Pickle module, then deserializes it back into `loaded_data` from the same file, printing the result.
Python3
import pickle
# Serialize
data = {'name': 'John', 'age': 30}
with open('data.pickle', 'wb') as file:
pickle.dump(data, file)
# Deserialize
with open('data.pickle', 'rb') as file:
serialized_data = file.read()
file.seek(0)
loaded_data = pickle.load(file)
print("Type of serialized data:", type(serialized_data))
print("\nDeserialized data:", loaded_data)
print("Type of deserialized data:", type(loaded_data))
OutputType of serialized data: <class 'bytes'>
Deserialized data: {'name': 'John', 'age': 30}
Type of deserialized data: <class 'dict'>
Serialize and Deserialize an Open File Object Using JSON
This Python script serializes a dictionary `data` into a JSON file named 'data.json', then deserializes it back into `loaded_data` from the same file, printing the result.
Python3
import json
# Serialize
data = {'name': 'John', 'age': 30}
with open('data.json', 'w') as file:
json.dump(data, file)
# Deserialize
with open('data.json', 'r') as file:
serialized_data = file.read()
file.seek(0)
loaded_data = json.load(file)
print("Type of serialized data:", type(serialized_data))
print("\nDeserialized data:", loaded_data)
print("Type of deserialized data:", type(loaded_data))
OutputType of serialized data: <class 'str'>
Deserialized data: {'name': 'John', 'age': 30}
Type of deserialized data: <class 'dict'>
Serialize and Deserialize an Open File Object Using YAML
This Python script serializes a dictionary `data` into a YAML file named 'data.yaml', then deserializes it back into `loaded_data` from the same file, printing the result.
Python3
import yaml
# Serialize
data = {'name': 'John', 'age': 30}
with open('data.yaml', 'w') as file:
yaml.dump(data, file)
# Deserialize
with open('data.yaml', 'r') as file:
serialized_data = file.read()
file.seek(0)
loaded_data = yaml.safe_load(file)
print("Type of serialized data:", type(serialized_data))
print("Deserialized data:", loaded_data)
print("Type of deserialized data:", type(loaded_data))
Output
Type of serialized data: <class 'str'>
Deserialized data: {'age': 30, 'name': 'John'}
Type of deserialized data: <class 'dict'>
Similar Reads
Python Loop through Folders and Files in Directory File iteration is a crucial process of working with files in Python. The process of accessing and processing each item in any collection is called File iteration in Python, which involves looping through a folder and perform operation on each file. In this article, we will see how we can iterate ove
4 min read
Create a File Path with Variables in Python The task is to create a file path using variables in Python. Different methods we can use are string concatenation and os.path.join(), both of which allow us to build file paths dynamically and ensure compatibility across different platforms. For example, if you have a folder named Documents and a f
3 min read
Check a File is Opened or Closed in Python In computer programming, working with files is something we often do. Python, a programming language, gives us useful tools to handle files. One important thing to know when dealing with files is whether a file is currently open or closed. This is crucial to avoid problems and make sure the data sta
4 min read
Extract Multiple JSON Objects from one File using Python Python is extremely useful for working with JSON( JavaScript Object Notation) data, which is a most used format for storing and exchanging information. However, it can become challenging when dealing with multiple JSON objects stored within a single file. In this article, we will see some techniques
3 min read
How to Load a File into the Python Console Loading files into the Python console is a fundamental skill for any Python programmer, enabling the manipulation and analysis of diverse data formats. In this article, we'll explore how to load four common file typesâtext, JSON, CSV, and HTMLâinto the Python console. Whether you're dealing with raw
4 min read