python: pickle 사용법

djlee·2023년 3월 21일
0

python에서 pickle을 사용하는 방법은 아주 간단해요.
아래와 같은 방법으로 저장 및 불러오기를 사용하면 돼요.

저장

import pickle

# An arbitrary collection of objects supported by pickle.
data = {
    'a': [1, 2.0, 3+4j],
    'b': ("character string", b"byte string"),
    'c': {None, True, False}
}

with open('data.pickle', 'wb') as f:
    # Pickle the 'data' dictionary using the highest protocol available.
    pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)

불러오기

import pickle

with open('data.pickle', 'rb') as f:
    # The protocol version used is detected automatically, so we do not
    # have to specify it.
    data = pickle.load(f)

참고

https://docs.python.org/ko/3.10/library/pickle.html

0개의 댓글