小编J. *_*ing的帖子

UnpicklingError:尝试从搁置文件中读取字典时,pickle 数据被截断

我是一名老师,我正在尝试编写一个简单的函数,将学生的电子邮件保存在字典中,以便在另一个程序中使用。我需要在多次执行中保存字典,因此我尝试使用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.') …
Run Code Online (Sandbox Code Playgroud)

python dictionary shelve pickle

6
推荐指数
1
解决办法
2万
查看次数

标签 统计

dictionary ×1

pickle ×1

python ×1

shelve ×1