cPickle下溢错误

时间:2013-03-28 20:36:58

标签: python file-io pickle

我正在尝试使用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 64bit

1 个答案:

答案 0 :(得分:0)

试试这个,它为我修正了错误:

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)
    f.close()