Using pickles in Python

I am trying to use pickle in Python and I am failing. For every error I overcome, I get a different message. The following message appears when I run my code. I am getting a syntax error for my use of "else". Why is this?

I'm new to Python but can't figure out what I am doing wrong?

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

      

+3


source to share


2 answers


Try using mode rb

when reading a file containing pickle:

file_mail = open('email.dat','rb')
email = pickle.load(file_mail)

      

And use mode wb

when writing the pickled obj view to an open file:



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

      

See the brine example for details .

+3


source


Thanks @McGrady! Here is the final answer I received.



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

      

0


source







All Articles