使用Pickle 儲存字典:綜合指南
問題:
問題:我怎麼有效地使用pickle 模組來序列化字典或任何其他Python物件?
答案:import pickle # Create your dictionary a = {'hello': 'world'} # Open a binary file for writing with open('filename.pickle', 'wb') as handle: # Serialize the dictionary using pickle.dump pickle.dump(a, handle, protocol=pickle.HIGHEST_PROTOCOL)
# Open the binary file for reading with open('filename.pickle', 'rb') as handle: # Deserialize the dictionary using pickle.load b = pickle.load(handle)
以上是如何使用 Pickle 模組序列化和反序列化 Python 字典?的詳細內容。更多資訊請關注PHP中文網其他相關文章!