使用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 24 Python
用Python将IP地址在整型和字符串之间轻松转换
Mar 22 Python
python将txt文件读取为字典的示例
Dec 22 Python
Django实现单用户登录的方法示例
Mar 28 Python
python仿抖音表白神器
Apr 08 Python
Django用户身份验证完成示例代码
Apr 03 Python
Pycharm IDE的安装和使用教程详解
Apr 30 Python
Python调用shell cmd方法代码示例解析
Jun 18 Python
如何对python的字典进行排序
Jun 19 Python
python批量合成bilibili的m4s缓存文件为MP4格式 ver2.5
Dec 01 Python
python mongo 向数据中的数组类型新增数据操作
Dec 05 Python
Python常用GUI框架原理解析汇总
Dec 07 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
php导出生成word的方法
2015/12/25 PHP
使用symfony命令创建项目的方法
2016/03/17 PHP
php简单获取复选框值的方法
2016/05/11 PHP
javascript 密码强弱度检测万能插件
2009/02/25 Javascript
高效率JavaScript编写技巧整理
2013/08/23 Javascript
浮动的div自适应居中显示的js代码
2013/12/23 Javascript
Js判断CSS文件加载完毕的具体实现
2014/01/17 Javascript
写出高效jquery代码的19条指南
2014/03/19 Javascript
JavaScript中的依赖注入详解
2015/03/18 Javascript
JavaScript中的getTimezoneOffset()方法使用详解
2015/06/10 Javascript
用Move.js配合创建CSS3动画的入门指引
2015/07/22 Javascript
Js获取图片原始宽高的实现代码
2016/05/17 Javascript
JSON字符串和对象相互转换实例分析
2016/06/16 Javascript
利用jQuery插件imgAreaSelect实现图片上传裁剪(放大缩小)
2016/12/02 Javascript
老生常谈js数据类型
2017/08/03 Javascript
在Vue.js中使用Mixins的方法
2017/09/12 Javascript
解析vue路由异步组件和懒加载案例
2018/06/08 Javascript
在Create React App中启用Sass和Less的方法示例
2019/01/16 Javascript
浅谈vue限制文本框输入数字的正确姿势
2019/09/02 Javascript
vue实现PC端分辨率适配操作
2020/08/03 Javascript
python中subprocess批量执行linux命令
2018/04/27 Python
python 函数内部修改外部变量的方法
2018/12/18 Python
Python Numpy库常见用法入门教程
2020/01/16 Python
TensorFlow tensor的拼接实例
2020/01/19 Python
python和node.js生成当前时间戳的示例
2020/09/29 Python
Lulu & Georgia官方网站:购买地毯、家具、抱枕、壁纸、床上用品等
2018/03/19 全球购物
Nike墨西哥官网:Nike MX
2020/08/30 全球购物
中间件分为哪几类
2012/03/14 面试题
怎样写演讲稿
2014/01/04 职场文书
科技工作者先进事迹
2014/08/16 职场文书
领导班子个人对照检查剖析材料
2014/09/29 职场文书
2015年财务人员工作总结
2015/04/10 职场文书
2015初中生物教研组工作总结
2015/07/21 职场文书
css3 利用transform-origin 实现圆点分布在大圆上布局及旋转特效
2021/04/29 HTML / CSS
Spring中的@Transactional的工作原理
2022/06/05 Java/Android
SpringBoot详解整合Redis缓存方法
2022/07/15 Java/Android