187. Object Serialization with pickle and json
1. Serialize and Deserialize with pickle
pickleimport pickle
data = {'name': 'Alice', 'age': 25, 'city': 'Wonderland'}
# Serialize
with open('data.pkl', 'wb') as f:
pickle.dump(data, f)
# Deserialize
with open('data.pkl', 'rb') as f:
loaded_data = pickle.load(f)
print(loaded_data)2. Serialize Multiple Objects with pickle
pickleimport pickle
data1 = [1, 2, 3]
data2 = {'a': 10, 'b': 20}
with open('multi_data.pkl', 'wb') as f:
pickle.dump(data1, f)
pickle.dump(data2, f)
with open('multi_data.pkl', 'rb') as f:
loaded_data1 = pickle.load(f)
loaded_data2 = pickle.load(f)
print(loaded_data1, loaded_data2)3. Serialize Custom Objects with pickle
pickle4. Serialize and Deserialize with json
json5. Serialize Python Objects to JSON String
6. Serialize Custom Objects with json Using default Argument
json Using default Argument7. Deserialize Custom JSON with object_hook
object_hook8. Use pickle for Serializing Large Data
pickle for Serializing Large Data9. Handling Encoding and Decoding Issues in json
json10. Serialize and Deserialize Nested Data Structures
Last updated