使用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 相关文章推荐
Centos5.x下升级python到python2.7版本教程
Feb 14 Python
python执行等待程序直到第二天零点的方法
Apr 23 Python
Python将一个CSV文件里的数据追加到另一个CSV文件的方法
Jul 04 Python
解决pycharm回车之后不能换行或不能缩进的问题
Jan 16 Python
python3使用matplotlib绘制条形图
Mar 25 Python
python使用adbapi实现MySQL数据库的异步存储
Mar 19 Python
解决django服务器重启端口被占用的问题
Jul 26 Python
Python 、Pycharm、Anaconda三者的区别与联系、安装过程及注意事项
Oct 11 Python
python命令 -u参数用法解析
Oct 24 Python
opencv python如何实现图像二值化
Feb 03 Python
Pytorch转tflite方式
May 25 Python
Python图像处理之图像拼接
Apr 28 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
无线电的诞生过程
2021/03/01 无线电
详解WordPress中的头像缓存和代理中的缓存更新方法
2016/03/01 PHP
PHP实现可自定义样式的分页类
2016/03/29 PHP
php打开本地exe程序,js打开本地exe应用程序,并传递相关参数方法
2018/02/06 PHP
PHP+redis实现微博的拉模型案例详解
2019/07/10 PHP
非常好的js代码
2006/06/27 Javascript
javascript showModalDialog 内跳转页面的问题
2010/11/25 Javascript
编写js扩展方法判断一个数组中是否包含某个元素
2013/11/08 Javascript
jQuery实现倒计时按钮功能代码分享
2014/09/03 Javascript
javascript使用正则获取url上的某个参数
2014/09/04 Javascript
JavaScript获取表单内所有元素值的方法
2015/04/02 Javascript
详解JavaScript中基于原型prototype的继承特性
2016/05/05 Javascript
Bootstrap表格使用方法详解
2017/02/17 Javascript
vue 实现复制内容到粘贴板clipboard的方法
2018/03/17 Javascript
vue router的基本使用和配置教程
2018/11/05 Javascript
JavaScript鼠标拖拽事件详解
2020/04/03 Javascript
vue fetch中的.then()的正确使用方法
2020/04/17 Javascript
[55:44]OG vs NAVI 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/17 DOTA
python将字典内容存入mysql实例代码
2018/01/18 Python
python sqlite的Row对象操作示例
2019/09/11 Python
pandas中遍历dataframe的每一个元素的实现
2019/10/23 Python
python 和c++实现旋转矩阵到欧拉角的变换方式
2019/12/04 Python
Python多线程threading join和守护线程setDeamon原理详解
2020/03/18 Python
Python实现代码块儿折叠
2020/04/15 Python
jupyter notebook读取/导出文件/图片实例
2020/04/16 Python
Pyinstaller打包Scrapy项目的实现步骤
2020/09/22 Python
基于css3的属性transition制作菜单导航效果
2015/09/01 HTML / CSS
HTML5 localStorage使用总结
2017/02/22 HTML / CSS
Craghoppers德国官网:户外和旅行服装
2020/02/14 全球购物
比赛口号大全
2014/06/10 职场文书
敬老院标语
2014/06/27 职场文书
竞选班干部演讲稿500字
2014/08/20 职场文书
装配车间主任岗位职责
2015/04/08 职场文书
生产车间管理制度
2015/08/04 职场文书
感恩老师主题班会
2015/08/12 职场文书
react国际化react-intl的使用
2021/05/06 Javascript