Pickling और Unpickling का मतलब Python में data को store और retrieve करना होता है।
Pickling
Pickling वो process है जिसमें Python objects (जैसे lists, dictionaries, etc.) को byte stream में बदल दिया जाता है ताकि आप उसे file में save कर सकें या network पर भेज सकें।
Unpickling
Unpickling उस byte stream को वापस original Python object में convert करने का process है, ताकि आप उसे फिर से use कर सकें।
Example:
मान लो, आपको एक Python dictionary को file में store करना है और बाद में वापस use करना है।
import pickle
# Python dictionary
data = {"name": "Rahul", "age": 25, "city": "Mumbai"}
# Pickling (Saving the object in a file)
with open('data.pkl', 'wb') as file:
pickle.dump(data, file)
print("Data saved successfully!")
# Unpickling (Reading 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': 'Rahul', 'age': 25, 'city': 'Mumbai'}
Explanation:
1. Pickling में dictionary को 'data.pkl' नाम की file में save कर दिया गया।
2. Unpickling में उसी file से data को वापस dictionary में convert करके use किया गया।
In short:
Pickling की मदद से
आप data को आसानी से store और share कर सकते हैं।
No comments:
Post a Comment