使用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程序设计入门(5)类的使用简介
Jun 16 Python
Python中下划线的使用方法
Mar 27 Python
Python实现线程池代码分享
Jun 21 Python
TensorFlow打印tensor值的实现方法
Jul 27 Python
Python拼接微信好友头像大图的实现方法
Aug 01 Python
selenium+python设置爬虫代理IP的方法
Nov 29 Python
对python过滤器和lambda函数的用法详解
Jan 21 Python
详解Python用户登录接口的方法
Apr 17 Python
Python PyCharm如何进行断点调试
Jul 05 Python
django orm模块中的 is_delete用法
May 20 Python
利用python下载scihub成文献为PDF操作
Jul 09 Python
Python实现灰色关联分析与结果可视化的详细代码
Mar 25 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的控制语句
2006/10/09 PHP
php基础知识:控制结构
2006/12/13 PHP
PHP采集腾讯微博的实现代码
2012/01/19 PHP
解析PHP中的内存管理,PHP动态分配和释放内存
2013/06/28 PHP
PHP错误Cannot use object of type stdClass as array in错误的解决办法
2014/06/12 PHP
destoon找回管理员密码的方法
2014/06/21 PHP
PHP+FastCGI+Nginx配置PHP运行环境
2014/08/07 PHP
PHP基于rabbitmq操作类的生产者和消费者功能示例
2018/06/16 PHP
详解PHP版本兼容之openssl调用参数
2018/07/25 PHP
Prototype 学习 工具函数学习($A方法)
2009/07/12 Javascript
JavaScript让IE浏览器event对象符合W3C DOM标准
2009/11/24 Javascript
IE本地存储userdata的一个bug说明
2010/07/01 Javascript
JavaScript中json对象和string对象之间相互转化
2012/12/26 Javascript
JS实现静止元素自动移动示例
2014/04/14 Javascript
JavaScript中的this引用(推荐)
2016/08/05 Javascript
使用ajaxfileupload.js实现上传文件功能
2016/08/13 Javascript
javascript 中的try catch应用总结
2017/04/01 Javascript
ztree简介_动力节点Java学院整理
2017/07/19 Javascript
swiper实现异形轮播效果
2019/11/28 Javascript
javascript前端和后台进行数据交互方法示例
2020/08/07 Javascript
jquery实现简单每周轮换的日历
2020/09/10 jQuery
给Python初学者的一些编程技巧
2015/04/03 Python
Python实现的下载网页源码功能示例
2017/06/13 Python
Python中列表与元组的乘法操作示例
2018/02/10 Python
python 2.7.13 安装配置方法图文教程
2018/09/18 Python
selenium 安装与chromedriver安装的方法步骤
2019/06/12 Python
Django shell调试models输出的SQL语句方法
2019/08/29 Python
Pandas实现DataFrame按行求百分数(比例数)
2019/12/27 Python
Python结合Window计划任务监测邮件的示例代码
2020/08/05 Python
加大码胸罩、内裤和服装:Just My Size
2019/03/21 全球购物
女性时尚网购:Chic Me
2019/07/30 全球购物
国际贸易个人求职信范文
2014/01/04 职场文书
普通话宣传标语
2014/06/26 职场文书
民主评议党员自我评议范文2014
2014/09/26 职场文书
公务员学习中国梦心得体会
2016/01/05 职场文书
python爬虫selenium模块详解
2021/03/30 Python