首页 > 编程语言 > 详细

Python序列化

时间:2016-09-17 14:59:52      阅读:271      评论:0      收藏:0      [点我收藏+]

通过pickle来序列化:

# -*- coding: utf-8 -*-
import pickle
#-------------------序列化---------------------
zoo_animals_1 = {
Unicorn : Cotton Candy House,
Sloth : Rainforest Exhibit,
Bengal Tiger : Jungle House,
Atlantic Puffin : Arctic Exhibit,
Rockhopper Penguin : Arctic Exhibit}

zoo_animals_2 = {
Unicorn 2 : Cotton Candy House,
Sloth 2 : Rainforest Exhibit,
Bengal Tiger 2 : Jungle House,
Atlantic Puffin 2 : Arctic Exhibit,
Rockhopper Penguin 2 : Arctic Exhibit}

#序列化。如果有多个数据要序列化,可以汇总到一个字典里。
all_data = {zoo1:zoo_animals_1, zoo2:zoo_animals_2}

pickle_file = open(list.pkl, wb)

pickle.dump(all_data, pickle_file, 0)
#pickle.dump(zoo_animals, administrator, 0)

pickle_file.close()

#-------------------反序列化---------------------
pickle_file = open(list.pkl, rb)

all_data = pickle.load(pickle_file)

pickle_file.close()

print all_data
print ---------------
zoo_animals_3 = all_data[zoo1]
print zoo_animals_3[Unicorn]

 

Python序列化

原文:http://www.cnblogs.com/ruanchao/p/5878754.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!