使用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连接mongodb操作数据示例(mongodb数据库配置类)
Dec 31 Python
Python中使用HTMLParser解析html实例
Feb 08 Python
python实现通过pil模块对图片格式进行转换的方法
Mar 24 Python
基于Python __dict__与dir()的区别详解
Oct 30 Python
使用Pyinstaller的最新踩坑实战记录
Nov 08 Python
python3写爬取B站视频弹幕功能
Dec 22 Python
Python贪心算法实例小结
Apr 22 Python
Python3最长回文子串算法示例
Mar 04 Python
pandas DataFrame 行列索引及值的获取的方法
Jul 02 Python
python wav模块获取采样率 采样点声道量化位数(实例代码)
Jan 22 Python
Python参数传递对象的引用原理解析
May 22 Python
Pytorch 使用opnecv读入图像由HWC转为BCHW格式方式
Jun 02 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
yii2.0之GridView自定义按钮和链接用法
2014/12/15 PHP
PHP实现断点续传乱序合并文件的方法
2018/09/06 PHP
javascript使用eval或者new Function进行语法检查
2010/10/16 Javascript
js jquery分别实现动态的文件上传操作按钮的添加和删除
2014/01/13 Javascript
jquery 删除字符串最后一个字符的方法解析
2014/02/11 Javascript
node.js中的require使用详解
2014/12/15 Javascript
基于jquery实现select选择框内容左右移动添加删除代码分享
2015/08/25 Javascript
Bootstrap选项卡与Masonry插件的完美结合
2016/07/06 Javascript
js实现四舍五入完全保留两位小数的方法
2016/08/02 Javascript
判断jQuery是否加载完成,没完成继续判断的解决方法
2017/12/06 jQuery
JS兼容所有浏览器的DOMContentLoaded事件
2018/01/12 Javascript
vue2 mint-ui loadmore实现下拉刷新,上拉更多功能
2018/03/21 Javascript
vue通过点击事件读取音频文件的方法
2018/05/30 Javascript
jQuery选择器之基本过滤选择器用法实例分析
2019/02/19 jQuery
layui 表单标签的校验方法
2019/09/04 Javascript
JavaScript this关键字指向常用情况解析
2020/09/02 Javascript
[05:15]2018年度CS GO社区贡献奖-完美盛典
2018/12/16 DOTA
python del()函数用法
2013/03/24 Python
python发送邮件实例分享
2017/07/28 Python
python读取图片的方式,以及将图片以三维数组的形式输出方法
2019/07/03 Python
命令行运行Python脚本时传入参数的三种方式详解
2019/10/11 Python
tensorflow安装成功import tensorflow 出现问题
2020/04/16 Python
基于CSS3的animation属性实现微信拍一拍动画效果
2020/06/22 HTML / CSS
HTML5 Canvas+JS控制电脑或手机上的摄像头实例
2014/05/03 HTML / CSS
意大利奢侈品网站:Italist
2016/08/23 全球购物
家庭户外服装:Hawkshead
2017/11/02 全球购物
Farah官方网站:男士服装及配件
2019/11/01 全球购物
2014三八妇女节活动总结范文四篇
2014/03/09 职场文书
关于保护环境的标语
2014/06/09 职场文书
2014向国旗敬礼网上签名活动总结
2014/09/27 职场文书
2014年街道办事处工作总结
2014/12/11 职场文书
鲁迅故里导游词
2015/02/05 职场文书
敬老院活动感想
2015/08/07 职场文书
Python快速实现一键抠图功能的全过程
2021/06/29 Python
Pygame Rect区域位置的使用(图文)
2021/11/17 Python
十大好看的穿越动漫排名:《瑞克和莫蒂》第一,国漫《有药》在榜
2022/03/18 日漫