使用pickle存储数据dump 和 load实例讲解
技术  /  管理员 发布于 7年前   258
使用pickle模块来dump你的数据:对上篇博客里的sketch.txt文件:
import osimport sysimport pickle man=[ ]other=[ ]try: data=open('sketch.txt') for each_line in data: try:(role,line_spoken)=each_line.split(':',1)line_spoken=line_spoken.strip()if role == 'Man': man.append(line_spoken)elif role == 'Other Man': other.append(line_spoken) except ValueError:pass data.close()except IOError: nester.print_lol('The data file is missing!') try: with open('man_data.txt','wb') as man_file: pickle.dump(man,man_file) with open('other_data.txt','wb') as other_file: pickle.dump(other,other_file) except IOError as err: print('File error: ' + str(err))except pickle.PickleError as perr: print('Pickling error: ' + str(perr))
打开man_data.txt,看结果:
]q (X' Is this the right room for an argument?qX No you haven't!qX When?qX No you didn't!qX You didn't!qX You did not!qX= Ah! (taking out his wallet and paying) Just the five minutes.qX You most certainly did not!qX Oh no you didn't!q X Oh no you didn't!qX Oh look, this isn't an argument!qX No it isn't!qX It's just contradiction!qX It IS!qX You just contradicted me!qX You DID!qX You did just then!qX" (exasperated) Oh, this is futile!!qX Yes it is!qe.
把已存储在man_data.txt上的二进制文件,恢复成可以读的文件,存放在new_man.txt中:
import nesterimport osimport sysimport pickle man=[ ]other=[ ]new_man=[ ] try: data=open('sketch.txt') for each_line in data: try:(role,line_spoken)=each_line.split(':',1)line_spoken=line_spoken.strip()if role == 'Man': man.append(line_spoken)elif role == 'Other Man': other.append(line_spoken) except ValueError:pass data.close()except IOError: print_lol('The data file is missing!') try:# with open('man_data.txt','wb') as man_file:# pickle.dump(man,man_file)# with open('other_data.txt','wb') as other_file:# pickle.dump(other,other_file) with open('man_data.txt','rb') as man_file: new_man=pickle.load(man_file) except IOError as err: print('File error: ' + str(err))except pickle.PickleError as perr: print('Pickling error: ' + str(perr))
查看结果:
RESTART: C:/Users/ThinkPad/AppData/Local/Programs/Python/Python36-32/chapter4-134-pickle.py >>> import nester>>> nester.print_lol(new_man)Is this the right room for an argument?No you haven't!When?No you didn't!You didn't!You did not!Ah! (taking out his wallet and paying) Just the five minutes.You most certainly did not!Oh no you didn't!Oh no you didn't!Oh look, this isn't an argument!No it isn't!It's just contradiction!It IS!You just contradicted me!You DID!You did just then!(exasperated) Oh, this is futile!!Yes it is!>>> import os>>> os.getcwd()'C:\\Users\\ThinkPad\\AppData\\Local\\Programs\\Python\\Python36-32'>>>
若是想保存new_man.txt到磁盘文件,可以加:
with open('new_man.txt','w') as new_man_file: nester.print_lol(new_man,fn=new_man_file)
以上这篇使用pickle存储数据dump 和 load实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号