0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pickleを使う

Posted at

大きめのデータオブジェクトをファイルに保存したい

※自分メモ

公式の標準ライブラリpickleを使う

※pickleはピクルスの意味。そう知ると一気にイメージが変わる。アメリカの大地の風景が思い浮かぶ

import pickle

保存する

  • "wb"でファイルopen
  • pickle.dump()で保存
val_a = 'value'

with open("./file_A", "wb") as f:
   pickle.dump(val_a ,f)

取り出す

  • 保存済みのファイルを"rb"でopen
  • pickle.load()で変数に読み込み
with open("./file_A", "rb") as f:
   val_a = pickle.load(f)

バイナリファイル、読出しでopen

複数まとめて保存する

val_a = 'a value'
list_a = ['a', 'b', 'c']
dict_a = {'key1':'val1', 'key2':'val2', 'key3':'val3'}

with open("./file_A", "wb") as f:
   pickle.dump([val_a, list_a, dict_a], f)

まとめて取り出す


with open("./file_A", "wb") as f:
   val, list, dict = pickle.dump([val_a, list_a, dict_a], f)
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?