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)