使用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脚本判断 Linux 是否运行在虚拟机上
Apr 25 Python
Python的Scrapy爬虫框架简单学习笔记
Jan 20 Python
Python的Flask框架应用程序实现使用QQ账号登录的方法
Jun 07 Python
详解Python3中字符串中的数字提取方法
Jan 14 Python
详解django中使用定时任务的方法
Sep 27 Python
DES加密解密算法之python实现版(图文并茂)
Dec 06 Python
python实现AES加密和解密
Mar 27 Python
PyQt5基本控件使用详解:单选按钮、复选框、下拉框
Aug 05 Python
python实现两个一维列表合并成一个二维列表
Dec 02 Python
在tensorflow以及keras安装目录查询操作(windows下)
Jun 19 Python
Django启动时找不到mysqlclient问题解决方案
Nov 11 Python
Python解析m3u8拼接下载mp4视频文件的示例代码
Mar 03 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
关于session在PHP5的配置文件中的详细设置参数说明
2011/04/20 PHP
PHP获取windows登录用户名的方法
2014/06/24 PHP
php 无限级分类,超级简单的无限级分类,支持输出树状图
2014/06/29 PHP
php解析url并得到url中的参数及获取url参数的四种方式
2015/10/26 PHP
PHP+MySql+jQuery实现的"顶"和"踩"投票功能
2016/05/21 PHP
php app支付宝回调(异步通知)详解
2018/07/25 PHP
javascript编程起步(第六课)
2007/02/27 Javascript
使用TextRange获取输入框中光标的位置的代码
2007/03/08 Javascript
AngularJs  Understanding Angular Templates
2016/09/02 Javascript
js基础之DOM中元素对象的属性方法详解
2016/10/28 Javascript
根据Bootstrap Paginator改写的js分页插件
2016/12/25 Javascript
Vue filter介绍及其使用详解
2017/10/21 Javascript
vue组件挂载到全局方法的示例代码
2018/08/02 Javascript
JS实现随机生成10个手机号的方法示例
2018/12/07 Javascript
vue中上传视频或图片或图片和文字一起到后端的解决方法
2019/12/01 Javascript
微信小程序实现单个卡片左滑显示按钮并防止上下滑动干扰功能
2019/12/06 Javascript
详解微信小程序中var、let、const用法与区别
2020/01/11 Javascript
Python读取Excel的方法实例分析
2015/07/11 Python
windows环境下tensorflow安装过程详解
2018/03/30 Python
利用python提取wav文件的mfcc方法
2019/01/09 Python
python调用百度地图WEB服务API获取地点对应坐标值
2019/01/16 Python
python爬虫 猫眼电影和电影天堂数据csv和mysql存储过程解析
2019/09/05 Python
python3中的eval和exec的区别与联系
2019/10/10 Python
selenium+python实现自动登陆QQ邮箱并发送邮件功能
2019/12/13 Python
基于Python实现人脸自动戴口罩系统
2020/02/06 Python
Python数组并集交集补集代码实例
2020/02/18 Python
在html页面中取得session中的值的方法
2020/08/11 HTML / CSS
Maje德国官网:法国女性成衣品牌
2017/02/10 全球购物
阿玛尼美妆俄罗斯官网:Giorgio Armani Beauty RU
2020/07/19 全球购物
公务员群众路线心得体会
2014/11/03 职场文书
铣工实训报告
2014/11/05 职场文书
岁月神偷观后感
2015/06/11 职场文书
2015年音乐教学工作总结
2015/07/22 职场文书
严以用权学习心得体会
2016/01/12 职场文书
《云雀的心愿》教学反思
2016/02/23 职场文书
大学迎新生的欢迎词
2019/06/25 职场文书