травление и выгрузка с использованием класса в python - PullRequest
0 голосов
/ 07 апреля 2020

пока в pickle.dumb информация (man, mod, price) сохраняется только последняя из них (cellphone.dat), и я хочу, чтобы все были сохранены, и pickle.load, если у меня более одного телефона. Может ли кто-нибудь помочь, пожалуйста?

class Cellphone :
    def __init__(self, manufacture, model, price):
        self.__manufacture = manufacture
        self.__model = model
        self.__price = price

    def set_manufacture(self, manufacture ):
        self.__manufacture = manufacture

    def set_model(self, model):
        self.__model = model

    def set_retail_price (self, price):
        self.__price = price

    def get_manufacture (self):
        return self.__manufacture
    def get_model (self):
        return self.__model
    def get_retail_price (self):
        return self.__price

Это блок кода для получения информации о дампе более чем одного телефона за итерацию


import cellphone
import pickle
filename = 'cellphone.dat'

def main():
    again = 'y'
    output_file =open(filename,'ab')

    while again.lower()=='y':
        man= input('man: ')
        mod= input('mod: ')
        price = float(input('price: '))

        phone= cellphone.Cellphone(man,mod,price)
        pickle.dump(phone,output_file)

        again= input('new phone? y/n ')
        # checking the content of each iteration
        print(phone.__dict__)

    print('#' * 30)
    # checking the content of the file
    print(phone.__dict__)
    output_file.close()

main()

этот блок кода для получения информации из сохраненного файла

import cellphone
import pickle

def main():

    end= False
    filename = 'cellphone.dat'
    with open(filename, 'rb') as fileinput:
        phone = pickle.load(fileinput)
        print(phone.__dict__)
        while not end:
            print(phone.get_manufacture())
            print(phone.get_model())
            print(phone.get_retail_price())
            end = True
main()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...