130. Serialization with pickle
1. Basic Serialization (Pickling)
import pickle
# Sample Python object (dictionary)
data = {'name': 'John', 'age': 30, 'city': 'New York'}
# Serialize the object and save it to a file
with open('data.pickle', 'wb') as file:
pickle.dump(data, file)
print("Object serialized and saved to data.pickle")2. Basic Deserialization (Unpickling)
import pickle
# Read the serialized object from the file
with open('data.pickle', 'rb') as file:
loaded_data = pickle.load(file)
print("Object deserialized from data.pickle:")
print(loaded_data)3. Serializing a Custom Class Object
4. Serializing Multiple Objects
5. Pickle with File Handling
6. Pickle with String (Memory Buffer)
7. Pickle Protocol
8. Pickle with Try-Except for Error Handling
9. Avoiding Pickle Insecurity
10. Using pickle with Python's with Statement
pickle with Python's with StatementKey Points:
Last updated