Lib Python (stdlib) para serialização e desserlização
1. Saving an object (serialization):
import pickle
# Example data
data = {'name': 'Alice', 'age': 30, 'city': 'New York'}
# Save the data to a file using pickle
with open('data.pkl', 'wb') as file:
pickle.dump(data, file)
print("Data saved successfully!")
In this example, the dictionary data
is saved to a file named data.pkl
using pickle.dump()
.
2. Loading an object (deserialization):
import pickle
# Load the data from the file using pickle
with open('data.pkl', 'rb') as file:
loaded_data = pickle.load(file)
print("Data loaded successfully!")
print(loaded_data)
This code reads the previously saved file data.pkl
and loads the dictionary back into the variable loaded_data
.
So you can save and retrieve Python objects easily using pickle
.