在Python中使用pickle

3

我正在尝试使用Python中的pickle,但是一直失败。每当我克服一个错误时,就会得到另一个错误消息。运行代码时,我会收到以下消息。我的'else'使用导致语法错误。为什么会这样?

我是Python的新手,但我想不出我做错了什么?

import pickle

def main():
    file_mail = open('email.dat','wb')
    save_data(file_mail)
    file_mail.close()


def save_data(file):
    email = {}

    count = 0

    while count == 0:
        print('Add an email: 1')
        print('Modify an email: 2')
        print('Delete an email: 3')
        print('Display the list: 4\n')

        choice = input('Enter a number from the list above:')

        if int(choice)== 1:
            name = input('Name:')
            mail = input('E-mail:')
            email[name] = mail
            print('Added Successfully')

            if int(choice) == 2:
                name = input('Name:')
                mail = input('E-mail:')
                email[name] = mail
                print('Modified Successfully')

                if int(choice) == 3:
                    name = input('Name:')
                    mail = input('E-mail:')
                    email[name] = mail
                    print('Deleted Successfully')

                    if int(choice) == 4:
                        print(email)

                        else:
                            print('Invalid selection')
                            c = input('Do you want to continue y/n: ')

                            if c.upper() == 'N':
                                count = 1
                                print('Invalid Letter')
                                file_mail = open('email.dat','wb')
                                pickle.dump(email,file_mail)
                                file_mail.close()
main()

2
当阅读(在您的第三行)时,您正在使用“'wb'”打开文件,这意味着写入二进制模式。将其更改为“'rb'”-读取二进制模式。 - umutto
2个回答

4
尝试使用rb模式读取pickle格式的文件:
file_mail = open('email.dat','rb')
email = pickle.load(file_mail)

当将对象的序列化表示写入到已打开的文件对象时,使用wb模式:

output = open('data.pkl', 'wb')
pickle.dump(data1, output)

更多详情请参考pickle示例


那是我的原始代码,但我得到了错误消息:email = pickle.load(file_mail) EOFError: Ran out of input。 - Part_Time_Nerd
@BWMustang13,你应该检查文件是否为空,这就是为什么你会收到“EOFError”错误的原因。 - McGrady
我相信我按照帖子建议运行了它,但是我仍然得到了相同的错误。 - Part_Time_Nerd
我更新了我的问题,采用分离i为两个单独函数的略微不同的方法。请参见上面的更改,但是我在else语句中遇到了无效的语法错误。这是为什么?可能是因为我已经看了18个小时的代码,但看起来缩进应该是正确的。 - Part_Time_Nerd
@BWMustang13,else语句的缩进错误。请查看43到52行的else部分。 - McGrady

0

感谢 @McGrady!这是我得到的最终答案。

import pickle

def main():
    file_mail = open('email.dat','wb')
    save_data(file_mail)
    file_mail.close()


def save_data(file):
    email = {}

    count = 0

    while count == 0:
        print('Add an email: 1')
        print('Modify an email: 2')
        print('Delete an email: 3')
        print('Display the list: 4\n')

        choice = input('\nEnter a number from the list above:')

        if int(choice)== 1:
            name = input('Name:')
            mail = input('E-mail:')
            email[name] = mail
            print('Added Successfully\n')

        else:
            if int(choice) == 2:
                name = input('Name:')
                mail = input('E-mail:')
                if name in email:
                    email[name] = mail
                    print('Modified Successfully\n')
                else:
                    print('Name not found')

            else:
                if int(choice) == 3:
                    name = input('Enter name you want to delete:')
                    if name in email:
                        email.pop(name)
                        print('Deleted Successfully\n')
                    else:
                       print('Name not found') 
                else:
                    if int(choice) == 4:
                        print(email)

                    else:
                        print('Invalid selection\n')
                        c = input('Do you want to continue y/n: ')
                        if c.upper() == 'N':
                            count = 1
                        else:
                            if c.upper() == 'N':
                                count = 1
                                print('Invalid Letter')

            file_mail = open('email.dat','wb')
            pickle.dump(email,file_mail)
            file_mail.close()
main()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接