码迷,mamicode.com
首页 > 其他好文 > 详细

pickle示例

时间:2018-06-16 11:47:39      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:date   sel   with   exception   set   /usr   file   dump   self   


my_pickle.py----------------------

#!/usr/bin/env python
# encoding: utf-8  
# Date: 2018/6/3
import pickle


class MyPickle:

    def __init__(self, filename):
        self.filename = filename

    def dump(self, obj):
        print(self.filename)
        with open(self.filename, ‘wb‘) as f:
            pickle.dump(obj, f)

    def load(self):
        with open(self.filename, ‘rb‘) as f:
            while True:
                try:
                    obj = pickle.load(f)
                    return obj
                except Exception as e:
                    print(e)
                    break

                    
settings.py----------------------------

#!/usr/bin/env python
# encoding: utf-8  
# Date: 2018/6/16


pickle_example = r‘pic_en‘

pickle_demo1.py  # 应用类------------------------

#!/usr/bin/env python
# encoding: utf-8  
# Date: 2018/6/16


# import my_pickle
from my_pickle import MyPickle
from settings import *

a_dict = {‘da‘: 111, 2: [23, 1, 4], ‘23‘: {1: 2, ‘d‘: ‘dad‘}}
my_exm = MyPickle(pickle_example)
my_exm.dump(a_dict)

m_dic = my_exm.load()
print(m_dic)
# {‘da‘: 111, 2: [23, 1, 4], ‘23‘: {1: 2, ‘d‘: ‘dad‘}}
                   

pickle示例

标签:date   sel   with   exception   set   /usr   file   dump   self   

原文地址:https://www.cnblogs.com/fmgao-technology/p/9189853.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!