使用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面相对象编程中对象的属性与类的方法
Jan 19 Python
Python模拟登陆淘宝并统计淘宝消费情况的代码实例分享
Jul 04 Python
对pandas的dataframe绘图并保存的实现方法
Aug 05 Python
Python元组及文件核心对象类型详解
Feb 11 Python
python 把文件中的每一行以数组的元素放入数组中的方法
Apr 29 Python
Pandas实现数据类型转换的一些小技巧汇总
May 07 Python
python 用所有标点符号分隔句子的示例
Jul 15 Python
用Python批量把文件复制到另一个文件夹的实现方法
Aug 16 Python
解决Pytorch 训练与测试时爆显存(out of memory)的问题
Aug 20 Python
使用python实现飞机大战游戏
Mar 23 Python
Tensorflow tf.tile()的用法实例分析
May 22 Python
python中如何设置代码自动提示
Jul 15 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
dede3.1分页文字采集过滤规则详说(图文教程)
2007/04/03 PHP
ThinkPHP页面跳转success与error方法概述
2014/06/25 PHP
thinkphp常见路径用法分析
2014/12/02 PHP
php简单判断两个字符串是否相等的方法
2015/07/13 PHP
PHP面向对象详解(三)
2015/12/07 PHP
微信公众号实现扫码获取微信用户信息(网页授权)
2019/04/09 PHP
firefox浏览器下javascript 拖动层效果与原理分析代码
2007/12/04 Javascript
基于JQuery 选择器使用说明介绍
2013/04/18 Javascript
js简单实现删除记录时的提示效果
2013/12/05 Javascript
AngularJS入门教程之Helloworld示例
2016/12/25 Javascript
jQuery使用siblings获取某元素所有同辈(兄弟姐妹)元素用法示例
2017/01/30 Javascript
JS简单判断函数是否存在的方法
2017/02/13 Javascript
JS实现异步上传压缩图片
2017/04/22 Javascript
AngularJS+bootstrap实现动态选择商品功能示例
2017/05/17 Javascript
jquery append与appendTo方法比较
2017/05/24 jQuery
详解如何在vscode里面调试js和node.js的方法步骤
2018/12/24 Javascript
vueScroll实现移动端下拉刷新、上拉加载
2019/03/22 Javascript
mpvue小程序循环动画开启暂停的实现方法
2019/05/15 Javascript
python with提前退出遇到的坑与解决方案
2018/01/05 Python
python 获取list特定元素下标的实例讲解
2018/04/09 Python
python根据url地址下载小文件的实例
2018/12/18 Python
python-opencv 将连续图片写成视频格式的方法
2019/01/08 Python
tensorflow查看ckpt各节点名称实例
2020/01/21 Python
Python使用xlrd实现读取合并单元格
2020/07/09 Python
详解pycharm连接远程linux服务器的虚拟环境的方法
2020/11/13 Python
新加坡网上化妆品店:Best Buy World
2018/05/18 全球购物
澳大利亚窗帘商店:Curtain Wonderland
2019/12/01 全球购物
面试后的感谢信范文
2014/02/01 职场文书
亲子读书活动方案
2014/02/22 职场文书
2014年清明节网上祭英烈寄语
2014/04/09 职场文书
共筑中国梦演讲稿
2014/04/23 职场文书
毕业晚宴祝酒词
2015/08/11 职场文书
CSS3 制作的图片滚动效果
2021/04/14 HTML / CSS
详解Apache SkyWalking 告警配置指南
2021/04/22 Servers
详解thinkphp的Auth类认证
2021/05/28 PHP
CSS实现切角+边框+投影+内容背景色渐变效果
2021/11/01 HTML / CSS