使用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中使用不同编码读写txt文件详解
May 28 Python
使用Python的Tornado框架实现一个Web端图书展示页面
Jul 11 Python
Django与JS交互的示例代码
Aug 23 Python
利用Python在一个文件的头部插入数据的实例
May 02 Python
python利用Tesseract识别验证码的方法示例
Jan 21 Python
Django web框架使用url path name详解
Apr 29 Python
我就是这样学习Python中的列表
Jun 02 Python
Django 使用easy_thumbnails压缩上传的图片方法
Jul 26 Python
Python 等分切分数据及规则命名的实例代码
Aug 16 Python
python super用法及原理详解
Jan 20 Python
Python列表解析操作实例总结
Feb 26 Python
python适合做数据挖掘吗
Jun 16 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
jQuery 使用手册(二)
2009/09/23 Javascript
Ext.FormPanel 提交和 Ext.Ajax.request 异步提交函数的区别
2009/11/12 Javascript
一个可绑定数据源的jQuery数据表格插件
2010/07/17 Javascript
深入理解JavaScript系列(2) 揭秘命名函数表达式
2012/01/15 Javascript
如何使用Javascript获取距今n天前的日期
2013/07/08 Javascript
js中prototype用法详细介绍
2013/11/14 Javascript
JavaScript中判断函数、变量是否存在
2015/06/10 Javascript
ReactJs快速入门教程(精华版)
2016/11/28 Javascript
AngularJS框架的ng-app指令与自动加载实现方法分析
2017/01/04 Javascript
vue实现单选和多选功能
2017/08/11 Javascript
JavaScript实现鼠标滚轮控制页面图片切换功能示例
2017/10/14 Javascript
Vue axios设置访问基础路径方法
2018/09/19 Javascript
axios对请求各种异常情况处理的封装方法
2018/09/25 Javascript
angular6 利用 ngContentOutlet 实现组件位置交换(重排)
2018/11/02 Javascript
React 源码中的依赖注入方法
2018/11/07 Javascript
微信小程序点击按钮动态切换input的disabled禁用/启用状态功能
2020/03/07 Javascript
[00:48]完美“圣”典2016风云人物:xiao8宣传片
2016/11/30 DOTA
[38:21]2018DOTA2亚洲邀请赛3月30日 小组赛A组 LGD VS Newbee
2018/03/31 DOTA
详解Python中的array数组模块相关使用
2016/07/05 Python
django实现前后台交互实例
2017/08/07 Python
python logging日志模块的详解
2017/10/29 Python
【python】matplotlib动态显示详解
2019/04/11 Python
关于Python作用域自学总结
2019/06/10 Python
python实现WebSocket服务端过程解析
2019/10/18 Python
pytorch 中的重要模块化接口nn.Module的使用
2020/04/02 Python
移动端html5 meta标签的神奇功效
2016/01/06 HTML / CSS
百联网上商城:i百联
2017/01/28 全球购物
《和我们一样享受春天》教学反思
2014/02/07 职场文书
优秀本科生求职推荐信
2014/02/24 职场文书
优秀班主任主要事迹材料
2014/12/16 职场文书
西安大雁塔导游词
2015/02/10 职场文书
2015年青年教师工作总结
2015/05/25 职场文书
2019辞职报告范本3篇!
2019/07/23 职场文书
教你如何用python开发一款数字推盘小游戏
2021/04/14 Python
go 实现简易端口扫描的示例
2021/05/22 Golang
windows server 2016 域环境搭建的方法步骤(图文)
2022/06/25 Servers