在 Python Pickle 文件中保存和加载多个对象
要将多个对象保存在 pickle 文件中,请按照以下步骤操作:
<code class="python">import pickle # Create a list of objects to be saved objects_to_save = [object1, object2, ...] # Open a binary file for writing with open('my_pickle_file', 'wb') as file: # Pickle each object and write it to the file for obj in objects_to_save: pickle.dump(obj, file)</code>
从 pickle 文件加载多个对象:
<code class="python"># Open the binary file for reading with open('my_pickle_file', 'rb') as file: # Load and print each object from the file while True: try: obj = pickle.load(file) print(obj) except EOFError: break</code>
其他注意事项:
以上是如何在 Python Pickle 文件中保存和加载多个对象?的详细内容。更多信息请关注PHP中文网其他相关文章!