Python pickle
Python 에서 pickling 한다는 것은 파이썬 빌트인 객체인 list, tuple 등을 binary 데이터로 만든다는 것이다.
Pickling 한 객체를 다시 파이썬 객체로 역직렬화하면 파이썬에서 바로 쓸 수 있는 객체가 된다.
import pickle
if __name__ == '__main__':
with open('test.bin', 'wb') as fw:
pickle.dump({'test': 'test', 'list': [(1, 2), {3, 5, 6}]}, fw, protocol=5)
with open('test.bin', 'rb') as fr:
result = pickle.load(fr)
print(result)
print(result['test'])
print(result['list'][1])
이를 활용하면 거의 변화하지 않아서 DB에 저장하긴 애매한 값들을 바이너리 파일로 가지고 있으면서, 필요할때마다 사용할 수 있다.
'Django,Python' 카테고리의 다른 글
[Django] 동시성 고려하기(1) - F 객체 사용하기 (0) | 2024.05.12 |
---|---|
[Django] factory_boy (0) | 2024.04.16 |
[Django] Custom Command 만들기 (0) | 2024.04.06 |
[Django] on_delete=CASCADE (1) | 2024.03.23 |
[Python] 파이썬의 typing (1) | 2024.03.09 |