pickle.dump()
是 Python 中的一個(gè)函數(shù),用于將對象序列化為二進(jìn)制數(shù)據(jù)流
pickle
模塊:import pickle
data = {
"name": "John",
"age": 30,
"city": "New York"
}
open()
函數(shù)以二進(jìn)制寫模式('wb'
)打開一個(gè)文件,用于存儲(chǔ)序列化后的數(shù)據(jù):with open("data_file.pkl", "wb") as file:
pickle.dump()
函數(shù),將對象序列化并寫入文件: pickle.dump(data, file)
將這些代碼片段組合在一起,完整示例如下:
import pickle
data = {
"name": "John",
"age": 30,
"city": "New York"
}
with open("data_file.pkl", "wb") as file:
pickle.dump(data, file)
此代碼將創(chuàng)建一個(gè)名為 data_file.pkl
的文件,其中包含序列化后的字典數(shù)據(jù)。