If you want to see in English and Hindi mix language about Pickling and Unpickling then click here
Pickling
Pickling is the process of converting a Python object into a byte stream (a format that can be stored in files or sent over a network).
Unpickling
Unpickling is the reverse process of converting the byte stream back into the original Python object so that it can be used again.
Example:
import pickle
# Python object: a dictionary
data = {"name": "Alice", "age": 30, "city": "New York"}
# Pickling (saving the object in a file)
with open('data.pkl', 'wb') as file:
pickle.dump(data, file)
print("Data saved successfully!")
# Unpickling (loading the object from the file)
with open('data.pkl', 'rb') as file:
loaded_data = pickle.load(file)
print("Loaded Data:", loaded_data)
Output:
Data saved successfully!
Loaded Data: {'name': 'Alice', 'age': 30, 'city': 'New York'}
Explanation:
1. Pickling: The dictionary is converted into a byte stream and saved in a file named 'data.pkl'.
2. Unpickling: The byte stream is read from the file and converted back into the original dictionary.
In short:
Pickling is useful when you want to store complex data (like lists, dictionaries, or objects) or send it across networks for later use.
No comments:
Post a Comment