我正在尝试使用cPickle加载文件,如下所示:

import cPickle

cPickle.load(open('test', 'rb'))

但是,我得到这个错误,
---------------------------------------------------------------------------
UnpicklingError                           Traceback (most recent call last)
<ipython-input-527-1b7653ff1917> in <module>()
----> 1 cPickle.load(open('test', 'rb'))

UnpicklingError: unpickling stack underflow

有时它会发出此错误
UnpicklingError: could not find MARK

我为什么要得到这个?

PS:cPickle无法与我一起使用任何文件,甚至是仅包含两个单词的新创建文件。我正在使用ubuntu 12.04和Python 64位

最佳答案

试试这个它为我修复了错误:

import pickle
import pandas as pd


# read in csv file to pandas dataframe and save as pickle file
training_data = pd.read_csv('train.csv')
pickle_out = open('train.pkl', 'wb')
pickle.dump(training_data, pickle_out)
pickle_out.close()


# open pickle file
file = 'train.pkl'
with open(file, 'rb') as f:
    training_data = pickle.load(f)

10-01 13:01