python - cPickle&pickle

load
###python2

def unpickle(file):
    import cPickle
    with open(file, 'rb') as fo:
        dict = cPickle.load(fo)
    return dict

###python3

def unpickle(file):
    import pickle
    with open(file, 'rb') as fo:
        dict = pickle.load(fo, encoding='bytes')
    return dict

save
python2

import cPickle
D = {'a': 1 , 'b': 2}
with open('datafile.pkl', 'wb') as f:
            cPickle.dump(D, f)

python3

D = {'a': 1 , 'b': 2}
F = open('datafile.pkl', 'wb')
import pickle
pickle.dump(D, F)
F.close()

你可能感兴趣的:(数据预处理)