首页 > 代码库 > pickle与shelve
pickle与shelve
pickle
Example
写入文件
import pickleintegers = [1, 2, 3, 4, 5]with open(‘pickle-example.p‘, ‘wb‘) as pfile: pickle.dump(integers, pfile)
读取文件
import picklewith open(‘pickle-example.p‘, ‘rb‘) as pfile: integers = pickle.load(pfile) print integers
shelve
Example
写入文件
import shelveintegers = [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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。