pickle.dump()
时间: 2024-05-05 22:17:26 浏览: 21
pickle.dump() is a method in Python's pickle module that serializes Python objects into a stream of bytes and saves them to a file or a file-like object. This method takes two arguments:
1. The Python object to be serialized
2. The file or file-like object to which the serialized data will be written
Here is an example of using pickle.dump():
```
import pickle
# create a Python object to serialize
my_data = {"name": "John", "age": 30, "hobby": "reading"}
# open a file for writing
with open("data.pickle", "wb") as f:
# serialize the Python object and save to file
pickle.dump(my_data, f)
```
In this example, the Python object `my_data` is serialized using `pickle.dump()` and saved to a file called "data.pickle". The file is opened in binary mode (`"wb"`) to ensure that the serialized data is written correctly.