| ���� Python �̳� | ��12�� ����/��� | |
|---|---|---|
| ��һҳ | ������ | ��һҳ |
Python�ṩһ������ģ�飬��Ϊpickle��ʹ�����������һ���ļ��д����κ�Python����֮�����ֿ�����������ȱ��ȡ�������ⱻ��Ϊ �־õ� �������
������һ��ģ���ΪcPickle�����Ĺ��ܺ�pickleģ����ȫ��ͬ��ֻ����������C���Ա�д�ģ����Ҫ��öࣨ��pickle��1000�����������ʹ�������е���һ���������������ォʹ��cPickleģ�顣��ס�����ǰ�������ģ�鶼���Ϊpickleģ�顣
#!/usr/bin/python
# Filename: pickling.py
import cPickle as p
#import pickle as p
shoplistfile = 'shoplist.data'
# the name of the file where we will store the object
shoplist = ['apple', 'mango', 'carrot']
# Write to the file
f = file(shoplistfile, 'w')
p.dump(shoplist, f) # dump the object to a file
f.close()
del shoplist # remove the shoplist
# Read back from the storage
f = file(shoplistfile)
storedlist = p.load(f)
print storedlist
��Դ�ļ���code/pickling.py��
$ python pickling.py
['apple', 'mango', 'carrot']
���ȣ���ע������ʹ����import..as�������һ�ֱ����������Ա������ǿ���ʹ�ø��̵�ģ�����ơ�����������У������������ܹ�ͨ���ظı�һ�о��л�����һ��ģ�飨cPickle����pickle�����ڳ�������ಿ�ֵ�ʱ�����Ǽذ����ģ���Ϊp��
Ϊ�����ļ��ﴢ��һ������������дģʽ��һ��file����Ȼ����ô�����ģ���dump�������Ѷ��浽���ļ��С�������̳�Ϊ ���� ��
������������ʹ��pickleģ���load�����ķ�����ȡ�ض���������̳�Ϊ ȡ���� ��
| ��һҳ | ��һ�� | ��һҳ |
|---|---|---|
| �ļ� | ��ҳ | ���� |