1. 程式人生 > >pickle 序列化與反序列化

pickle 序列化與反序列化

廢話不多說,直接上程式碼

import pickle

class Person:
    def __init__(self, name, age, job):
        self.name = name
        self.age = age
        self.job = job

    def work(self):
        print(self.name)
        print(self.age)
        print(self.job)
        print('*'*40)

if __name__ == '__main__':
    a_person = Person('abc', 22, 'waiter')
    with open('person.pkl', 'wb') as f:
        pickle.dump(a_person, f)
        
    with open('person.pkl', 'rb') as f:
        a_person = pickle.load(f)