首页 > 代码库 > Python序列化
Python序列化
通过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 = http://www.mamicode.com/{‘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_dataprint ‘---------------‘zoo_animals_3 = all_data[‘zoo1‘]print zoo_animals_3[‘Unicorn‘]
Python序列化
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。