[Python] Pickle : Object Serialization

llunaB·2022년 2월 22일

dataScience

목록 보기
5/7

Picke module

Pickle is used for serializing and de-serializing Python object structures.

Serialization?

Serialization refers to the process of converting an object in memory to a byte stream that can be stored on disk or sent over a network.

Deserialization?

Later on, this character stream can then be retrieved and de-serialized back to a Python object.

Pickling files

  • wb : write + binary
  • rb : read + binary
import pickle

dogs_dict = {'Ozzy': 3, 'Filou': 8, 'Luna': 5, 'Skippy': 10, 'Barco': 12, 'Balou': 9, 'Laika': 16 }
filename = 'dogs.txt'

# Pickling files : write data
outfile = open(filename, 'wb')
pickle.dump(dogs_dict, outfile)  # object, file
outfile.close()


# Unpickling files : read data
infile = open(filename, 'rb')
new_dict = pickle.load(infile)
infile.close()

print(new_dict)
print(new_dict == dogs_dict)

참고자료
https://www.datacamp.com/community/tutorials/pickle-python-tutorial#what

profile
안녕하세요. 성장하는 주니어 개발자입니다. :-)

0개의 댓글