2、因为存储的是对象,必须使用二进制形式写进文件
#!/usr/bin/python
# Filename: pickling.py
import pickle as p
#import pickle as p
shoplistfile = 'shoplist.data'
# the name of the file where we willl store the object
shoplist = ['apple', 'mango', 'carrot']
# Write to the file
f = open(shoplistfile, 'wb') #二进制打开
p.dump(shoplist, f)
f.close()
del shoplist # remove the shoplist
# Read back from the storage
f = open(shoplistfile, 'rb')
storedlist = p.load(f)
print(storedlist)
本文介绍如何使用Python的pickle模块来序列化和反序列化对象。pickle模块允许将Python对象保存到磁盘上,并且能够在后续读取这些对象,这对于持久化存储Python数据结构非常有用。文章通过一个简单的例子演示了如何保存和加载一个列表。
&spm=1001.2101.3001.5002&articleId=25136909&d=1&t=3&u=e0203c924b3447ba96d1ca41cf469e27)
729

被折叠的 条评论
为什么被折叠?



