2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

pickleについて

Last updated at Posted at 2023-09-03

pickle

pickleは、Pythonプログラムで記述したオブジェクトなどをファイル出力するためのライブラリ

import pickle
x = [1,2,3,4]

# save
with open('pickle_file.pkl', 'wb') as f:
    pickle.dump(x, f)

# load
with open('pickle_file.pkl', 'rb') as f:
    y = pickle.load(f)

複数オブジェクトを保存する

同じファイルに複数回書き込んだ場合、ロードも同じ回数実行するとすべてのオブジェクトがロードできる。
(1ファイルに複数回書き込むより、ファイル自体を分けたほうがいいような気がする。)

import pickle
a = [1,2,3,4]
b = 'pickle pickle pickle'

# save
with open('pickle_file.pkl', 'wb') as f:
    pickle.dump(a, f)
    pickle.dump(b, f)

# load
with open('pickle_file.pkl', 'rb') as f:
    x = pickle.load(f)
    y = pickle.load(f)
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?