I'm trying to use Pickle to save a dictionary in a file. The code to save the dictionary runs without any problems, but when I try to retrieve the dictionary from the file in the Python shell, I get an EOF error:
>>> import pprint
>>> pkl_file = open('data.pkl', 'rb')
>>> data1 = pickle.load(pkl_file)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/pickle.py", line 1378, in load
return Unpickler(file).load()
File "/usr/lib/python2.7/pickle.py", line 858, in load
dispatch[key](self)
File "/usr/lib/python2.7/pickle.py", line 880, in load_eof
raise EOFError
EOFError
My code is below.
It counts the frequency of each word and the date of the data (the date is the file name.) then saves words as keys of dictionary and the tuple of (freq,date) as values of each key. Now I want to use this dictionary as the input of another part of my work :
def pathFilesList():
source='StemmedDataset'
retList = []
for r,d,f in os.walk(source):
for files in f:
retList.append(os.path.join(r, files))
return retList
def parsing():
fileList = pathFilesList()
for f in fileList:
print "Processing file: " + str(f)
fileWordList = []
fileWordSet = set()
fw=codecs.open(f,'r', encoding='utf-8')
fLines = fw.readlines()
for line in fLines:
sWord = line.strip()
fileWordList.append(sWord)
if sWord not in fileWordSet:
fileWordSet.add(sWord)
for stemWord in fileWordSet:
stemFreq = fileWordList.count(stemWord)
if stemWord not in wordDict:
wordDict[stemWord] = [(f[15:-4], stemFreq)]
else:
wordDict[stemWord].append((f[15:-4], stemFreq))
fw.close()
if __name__ == "__main__":
parsing()
output = open('data.pkl', 'wb')
pickle.dump(wordDict, output)
output.close()
What do you think the problem is?