1
1

More than 3 years have passed since last update.

pickle

Last updated at Posted at 2020-03-08

pythonのデータをそのまま保存できます。

import pickle


class T(object):
    def __init__(self, name):
        self.name = name

data={
    'a': [1, 2, 3],
    'b': ('test', 'test'),
    'c': {'key': 'value'},
    'd': T('test')
}

#バイナリで書き込み
with open('data.pickle', 'wb') as f:
    pickle.dump(data, f)

with open('data.pickle','rb') as f:
    data_loaded = pickle.load(f)
    print(data_loaded)

出力:

{'a': [1, 2, 3], 'b': ('test', 'test'), 'c': {'key': 'value'}, 'd': <__main__.T object at 0x1145d0c50>}
1
1
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
1
1