使用pickle存储数据dump 和 load实例讲解


Posted in Python onDecember 30, 2019

使用pickle模块来dump你的数据:对上篇博客里的sketch.txt文件:

import os
import sys
import 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!q
X  Oh look, this isn't an argument!qX  No it isn't!qX  It's just contradiction!q
X  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 nester
import os
import sys
import 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实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python栈类实例分析
Jun 15 Python
深入理解python中的闭包和装饰器
Jun 12 Python
Python中标准模块importlib详解
Apr 16 Python
利用Python将每日一句定时推送至微信的实现方法
Aug 13 Python
Python subprocess库的使用详解
Oct 26 Python
PyQtGraph在pyqt中的应用及安装过程
Aug 04 Python
django-csrf使用和禁用方式
Mar 13 Python
Python 操作 PostgreSQL 数据库示例【连接、增删改查等】
Apr 21 Python
Python实现汇率转换操作
May 03 Python
Python 发送邮件方法总结
Aug 10 Python
python 30行代码实现蚂蚁森林自动偷能量
Feb 08 Python
一行代码python实现文件共享服务器
Apr 22 Python
在Python中利用pickle保存变量的实例
Dec 30 #Python
python Popen 获取输出,等待运行完成示例
Dec 30 #Python
Python3常见函数range()用法详解
Dec 30 #Python
Python Pickle 实现在同一个文件中序列化多个对象
Dec 30 #Python
python使用HTMLTestRunner导出饼图分析报告的方法
Dec 30 #Python
用python爬取历史天气数据的方法示例
Dec 30 #Python
pytorch 自定义卷积核进行卷积操作方式
Dec 30 #Python
You might like
基于OpenCV的PHP图像人脸识别技术
2009/10/11 PHP
解析PHP实现多进程并行执行脚本
2013/06/18 PHP
thinkphp路由规则使用示例详解和伪静态功能实现(apache重写)
2014/02/24 PHP
php使用codebase生成随机数
2014/03/25 PHP
memcache一致性hash的php实现方法
2015/03/05 PHP
php实现字符串反转输出的方法
2015/03/14 PHP
php使用Jpgraph创建3D饼形图效果示例
2017/02/15 PHP
js异步加载的三种解决方案
2013/03/04 Javascript
Extjs3.0 checkboxGroup 动态添加item实现思路
2013/08/14 Javascript
减少访问DOM的次数提升javascript性能
2014/02/24 Javascript
jQuery中offsetParent()方法用法实例
2015/01/19 Javascript
原生Js实现简易烟花爆炸效果的方法
2015/03/20 Javascript
JS打印组合功能
2016/08/04 Javascript
JS原型与原型链的深入理解
2017/02/15 Javascript
nodejs使用express获取get和post传值及session验证的方法
2017/11/09 NodeJs
three.js实现3D模型展示的示例代码
2017/12/31 Javascript
小程序获取当前位置加搜索附近热门小区及商区的方法
2019/04/08 Javascript
js+html5 canvas实现ps钢笔抠图
2019/04/28 Javascript
bootstrap datepicker的基本使用教程
2019/07/09 Javascript
layui prompt 设置允许空白提交的方法
2019/09/24 Javascript
OpenLayer学习之自定义测量控件
2020/09/28 Javascript
Ant design vue中的联动选择取消操作
2020/10/31 Javascript
跟老齐学Python之有容乃大的list(4)
2014/09/28 Python
python实现搜索指定目录下文件及文件内搜索指定关键词的方法
2015/06/28 Python
用python实现k近邻算法的示例代码
2018/09/06 Python
python opencv 检测移动物体并截图保存实例
2020/03/10 Python
用CSS3写的模仿iPhone中的返回按钮
2015/04/04 HTML / CSS
欧尚俄罗斯网上超市:Auchan俄罗斯
2018/05/03 全球购物
红色连衣裙精品店:Red Dress Boutique
2018/08/11 全球购物
New delete 与malloc free 的联系与区别
2013/02/04 面试题
音乐专业应届生教师求职信
2013/11/04 职场文书
2013年学期结束动员演讲稿
2014/01/07 职场文书
教师队伍管理制度
2014/01/14 职场文书
社区志愿者活动总结
2014/06/26 职场文书
党的生日活动方案
2014/08/15 职场文书
我们的节日中秋节活动总结
2015/03/23 职场文书