python pickle

#!/usr/bin/python
# -*- coding: UTF-8 -*-
# @date: 2017/7/22 23:41
# @name: Python_learn
# @author:vickey-wu

import os
import json
try:
import cPickle as pickle
except:
import pickle

test_dict = {"name":"vickey","age":18}
test_dict2 = dict(name = "vickey", age = 18)

#serialization a dict
dump_write = open("dump.txt","wb")
# pickle.dump(test_dict,dump_write)
test_dict_write = pickle.dump(test_dict,dump_write)
dump_write.close()

#deserialization a dict to dict
dump_read = open("dump.txt","rb")
test_dict_read = pickle.load(dump_read)
print test_dict_read

#serialization to json str
json_serial = json.dumps(test_dict)

#deserialization json str to dict
json_deserial = json.loads(json_serial)
print json_serial
print json_deserial
print type(json_serial),type(json_deserial),type(test_dict_write),type(test_dict_read)
posted @ 2017-07-23 00:30  随便了888  阅读(230)  评论(0编辑  收藏  举报