【问题标题】:UnpicklingError: pickle data was truncated when trying to read a dictionary from a shelved fileUnpicklingError:尝试从搁置文件中读取字典时,pickle 数据被截断
【发布时间】:2017-11-07 15:39:11
【问题描述】:

我是一名教师,我正在尝试编写一个简单的函数,将学生的电子邮件保存在字典中,以便在另一个程序中使用。我需要在多次执行中保存字典,所以我尝试使用shelve 来保存它;但是,在第二次运行该函数后,我收到一个 unpickling 错误,提示 pickle 数据被截断。代码如下:

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()

在运行程序之前,我在 shell 中创建了空字典shelfFile['studentEmails']。它第一次运行良好,但是当我尝试将shelfFile分配回studentEmails时给我_pickle.UnpicklingError: pickle data was truncated错误。我是这方面的新手,还在学习,所以非常感谢您的帮助。

【问题讨论】:

    标签: python dictionary pickle shelve


    【解决方案1】:

    我也遇到了同样的问题,经过一番调查后,我意识到这可能是因为我像个混蛋一样停止了我的程序(在使用搁置的过程中终止了它)。

    所以我删除了我的搁置并重新创建它,一切正常。

    我假设您也遇到了同样的错误,也许您通过终止程序或其他方式退出了无限 while 循环?

    【讨论】:

    • (这篇文章似乎没有为问题提供quality answer。请编辑您的答案,或者将其作为对问题的评论发布)。
    • 它提供了对他的错误信息的解释和解决方法,我很乐意收到这样的答案。特别是如果我的问题在一年前被问到并且我已经找到了解决方法。我尝试发表评论,但遗憾的是这个网站不允许新来者使用(“你必须有 50 名声望才能发表评论”)。
    • 每当书架有更新时,我都会使用 Shelf.sync() ,我成功解决了这个问题
    【解决方案2】:

    在玩弄了一些东西并阅读了一些其他网站之后,我能够使用pickle 而不是shelve 来实现我想要的。下面是代码现在的样子:

    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()
    

    这很适合我正在做的事情。我必须在 shell 中使用占位符创建 studentEmails 字典,因为 pickle 不允许空字典。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      • 2017-11-22
      • 2013-01-23
      • 2020-12-27
      • 1970-01-01
      • 2021-07-15
      • 1970-01-01
      相关资源
      最近更新 更多