python 之pickle模块--输出保存到文本(基本的数据序列和反序列化)

字典输出到文本该如何输出呢?遍历字典,输出也麻烦了,而且字典没有按顺序存储,读起来也麻烦。这个需求,导致的结果就是发现了这个好用的模块。

下面是一个字典的实例:

#!/usr/bin/env python
#-*- coding: utf-8 -*-
#filename:pickle_use.py

import pickle, pprint


def pickle_save():#使用pickle模块将数据对象保存到文件
    contact = {'julius1':{'cellphone':'13800000000','tel':'0512-34343534','qq':'354564656'},
               'julius2':{'cellphone':'13300000000','tel':'0513-34343534','qq':'454564656'},
               'julius3':{'cellphone':'13400000000','tel':'0514-34343534','qq':'554564656'},
               'julius4':{'cellphone':'13500000000','tel':'0515-34343534','qq':'654564656'}
               }
    f = open('contact_list.txt','w')
    pickle.dump(contact,f)
    f.close()

def pickle_load():#使用pickle从文件中重构python对象
    f = open('contact_list.txt','r')
    contact = pickle.load(f)
    pprint.pprint(contact)
    pprint.pprint(contact['julius2'])#从dict中按要求取值
    pprint.pprint(contact['julius3']['cellphone'])
    pprint.pprint(contact['julius4']['qq'])
    f.close()

pickle_save()
pickle_load()

输出:

contact_list.txt:

(dp0
S'julius4'
p1
(dp2
S'qq'
p3
S'654564656'
p4
sS'cellphone'
p5
S'13500000000'
p6
sS'tel'
p7
S'0515-34343534'
p8
ssS'julius1'
p9
(dp10
g3
S'354564656'
p11
sg5
S'13800000000'
p12
sg7
S'0512-34343534'
p13
ssS'julius3'
p14
(dp15
g3
S'554564656'
p16
sg5
S'13400000000'
p17
sg7
S'0514-34343534'
p18
ssS'julius2'
p19
(dp20
g3
S'454564656'
p21
sg5
S'13300000000'
p22
sg7
S'0513-34343534'
p23
ss.
从文件读取:

>>> ================================ RESTART ================================
>>> 
{'julius1': {'cellphone': '13800000000',
             'qq': '354564656',
             'tel': '0512-34343534'},
 'julius2': {'cellphone': '13300000000',
             'qq': '454564656',
             'tel': '0513-34343534'},
 'julius3': {'cellphone': '13400000000',
             'qq': '554564656',
             'tel': '0514-34343534'},
 'julius4': {'cellphone': '13500000000',
             'qq': '654564656',
             'tel': '0515-34343534'}}
{'cellphone': '13300000000', 'qq': '454564656', 'tel': '0513-34343534'}
'13400000000'
'654564656'
>>> 
 基本接口:

  pickle.dump(obj, file, [,protocol])

This is equivalent to Pickler(file, protocol).dump(obj).

  注解:将对象obj保存到文件file中去。
     protocol为序列化使用的协议版本,0:ASCII协议,所序列化的对象使用可打印的ASCII码表示;1:老式的二进制协议;2:2.3版本引入的新二进制协议,较以前的更高效。其中协议0和1兼容老版本的python。protocol默认值为0。
     file:对象保存到的类文件对象。file必须有write()接口, file可以是一个以'w'方式打开的文件或者一个StringIO对象或者其他任何实现write()接口的对象。如果protocol>=1,文件对象需要是二进制模式打开的。

  pickle.load(file)

This is equivalent to Unpickler(file).load().

  注解:从file中读取一个字符串,并将它重构为原来的python对象。
  file:类文件对象,有read()和readline()接口。

The following types can be pickled:

None, True, and False
integers, long integers, floating point numbers, complex numbers
normal and Unicode strings
tuples, lists, sets, and dictionaries containing only picklable objects
functions defined at the top level of a module
built-in functions defined at the top level of a module
classes that are defined at the top level of a module
instances of such classes whose __dict__ or the result of calling __getstate__() is picklable (see section The pickle protocol for details).



很好用的一个模块,支持很多类型的对象保存。

http://docs.python.org/2/library/pickle.html?highlight=pickle#pickle


你可能感兴趣的:(python学习记录)