UnpicklingError: pickle data was truncated when trying to read a dictionary from a shelved file
Asked Answered
N

2

7

I'm a teacher, and I'm trying to write a simple function that saves my students' emails in a dictionary for use in another program. I need the dictionary to be saved across multiple executions, so I'm trying to use shelve to save it; however, after running the function for a second time, I get an unpickling error saying the pickle data was truncated. Here is the code:

shelfFile = shelve.open('mydata')
studentEmails = shelfFile['studentEmails']
def inputEmails():
    while True:
        nameInput = input('Name: ')
        if nameInput == '':
            break
        emailInput = input('Email: ')
        if emailInput == '':
            print('Email not entered. Please try again.')
            continue
        while True:
            print('Is this information correct? [Y]es or [N]o')
            print('Name: ' + nameInput)
            print('Email: ' + emailInput)
            correctChoice = input('[Y] or [N]: ').upper()
            if correctChoice == 'Y':
                studentEmails[nameInput] = emailInput
                break
            elif correctChoice == 'N':
                print('Okay. Please input again.')
                break
            else:
                print('I did not understand that response.')
inputEmails()
shelfFile['studentEmails']=studentEmails
shelfFile.close()

I create the empty dictionary shelfFile['studentEmails'] in the shell before I run the program. It will run fine the first time, but give me the _pickle.UnpicklingError: pickle data was truncated error when I try to assign the shelfFile back to studentEmails. I'm new at this and still learning, so I appreciate the help.

Nachison answered 7/11, 2017 at 15:39 Comment(0)
G
2

I just had the same problem, and after a little investigation I realized it probably happened because I stopped my program like a jerk (terminated it in the middle of using the shelve).

So I deleted my shelve and created it again and everything worked fine.

I assume you had the same error, maybe you exited your infinite while loops by terminating the program or something?

Gentianaceous answered 16/6, 2018 at 16:13 Comment(2)
It provides an explanation to his error message and a way to solve it, I would love receiving such answers. Especially if my question was asked about an year ago and I already found a workaround. I tried commenting but sadly this site doesn't allow it for new comers ("You must have 50 reputation to comment").Gentianaceous
whenever there is an update to the Shelf, I followed with a Shelf.sync() , and I succeed to get rid of the issueLost
N
0

After toying around with things and reading a few other websites, I was able to achieve what I wanted using pickle instead of shelve. Here is what the code looks like now:

import pickle
loadData = open('saveData.p','rb')
studentEmails = pickle.load(loadData)
loadData.close()
def inputEmails():
    while True:
        nameInput = input('Name: ')
        if nameInput == '':
            break
        emailInput = input('Email: ')
        if emailInput == '':
            print('Email not entered. Please try again.')
            continue
        while True:
            print('Is this information correct? [Y]es or [N]o')
            print('Name: ' + nameInput)
            print('Email: ' + emailInput)
            correctChoice = input('[Y] or [N]: ').upper()
            if correctChoice == 'Y':
                studentEmails[nameInput] = emailInput
                break
            elif correctChoice == 'N':
                print('Okay. Please input again.')
                break
            else:
                print('I did not understand that response.')
inputEmails()
saveData = open('saveData.p','wb')
pickle.dump(studentEmails,saveData)
saveData.close()

This works just fine for what I'm doing. I had to create the studentEmails dictionary in the shell with placeholders as pickle doesn't allow for empty dictionaries.

Nachison answered 7/11, 2017 at 17:28 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.