首页 > 其他 > 详细

pickle与shelve

时间:2016-11-26 03:23:54      阅读:413      评论:0      收藏:0      [点我收藏+]

pickle Example

写入文件

import pickle

integers = [1, 2, 3, 4, 5]

with open(pickle-example.p, wb) as pfile:
    pickle.dump(integers, pfile)

读取文件

import pickle

with open(pickle-example.p, rb) as pfile:
    integers = pickle.load(pfile)
    print integers

shelve Example

写入文件

import shelve

integers = [1, 2, 3, 4, 5]

# If you‘re using Python 2.7, import contextlib and use
# the line:
# with contextlib.closing(shelve.open(‘shelf-example‘, ‘c‘)) as shelf:
with shelve.open(shelf-example, c) as shelf:
    shelf[ints] = integers

读取文件

import shelve

# If you‘re using Python 2.7, import contextlib and use
# the line:
# with contextlib.closing(shelve.open(‘shelf-example‘, ‘r‘)) as shelf:
with shelve.open(shelf-example, r) as shelf:
    for key in shelf.keys():
        print(repr(key), repr(shelf[key])))

 

pickle与shelve

原文:http://www.cnblogs.com/zhanmeiliang/p/6103478.html

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