使用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常见文件操作的函数示例代码
Nov 15 Python
python实现socket端口重定向示例
Feb 10 Python
python实现数据预处理之填充缺失值的示例
Dec 22 Python
使用Python和xlwt向Excel文件中写入中文的实例
Apr 21 Python
对Python3 解析html的几种操作方式小结
Feb 16 Python
Python 20行简单实现有道在线翻译的详解
May 15 Python
基于python调用psutil模块过程解析
Dec 20 Python
Python Sphinx使用实例及问题解决
Jan 17 Python
Django REST framwork的权限验证实例
Apr 02 Python
Python基础类继承重写实现原理解析
Apr 03 Python
基于Python制作一副扑克牌过程详解
Oct 19 Python
用Python实现童年贪吃蛇小游戏功能的实例代码
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
古巴咖啡 Cubita琥爵咖啡 独特的加勒比海风味咖啡
2021/03/06 新手入门
php 日期和时间的处理-郑阿奇(续)
2011/07/04 PHP
php GUID生成函数和类
2014/03/10 PHP
PHP date函数常用时间处理方法
2015/05/11 PHP
如何使用php等比例缩放图片
2016/10/12 PHP
PHP 文件上传后端处理实用技巧方法
2017/01/06 PHP
PHP实现chrome表单请求数据转换为接口使用的json数据
2021/03/04 PHP
javascript十个最常用的自定义函数(中文版)
2009/09/07 Javascript
一个收集图片的bookmarlet(js 刷新页面中的图片)
2010/05/27 Javascript
js实现仿MSN带关闭功能的右下角弹窗代码
2015/09/04 Javascript
深入浅析同源策略和跨域访问
2015/11/26 Javascript
浅析BootStrap Treeview的简单使用
2016/10/12 Javascript
JavaScript使用链式方法封装jQuery中CSS()方法示例
2017/04/07 jQuery
详解用vue编写弹出框组件
2017/07/04 Javascript
Angular X中使用ngrx的方法详解(附源码)
2017/07/10 Javascript
React Native实现进度条弹框的示例代码
2017/07/17 Javascript
JavaScript 数组的进化与性能分析
2017/09/18 Javascript
Javascript 之封装(Package)
2018/09/14 Javascript
微信小程序控制台提示warning:Now you can provide attr "wx:key" for a "wx:for" to improve performance解决方法
2019/02/21 Javascript
微信小程序搭建自己的Https服务器
2019/05/02 Javascript
Angular2实现的秒表及改良版示例
2019/05/10 Javascript
关于JS模块化的知识点分享
2019/10/16 Javascript
微信小程序如何实现点击图片放大功能
2020/01/21 Javascript
解决vue初始化项目一直停在downloading template的问题
2020/11/09 Javascript
基于Python的接口测试框架实例
2016/11/04 Python
使用Python实现简单的服务器功能
2017/08/25 Python
Python的argparse库使用详解
2018/10/09 Python
Python django搭建layui提交表单,表格,图标的实例
2019/11/18 Python
python语言是免费还是收费的?
2020/06/15 Python
前端面试必备之html5的新特性
2017/09/05 HTML / CSS
Linux如何为某个操作添加别名
2013/03/01 面试题
大班亲子运动会方案
2014/06/10 职场文书
舞蹈专业求职信
2014/06/13 职场文书
农村党员学习党的群众路线教育实践活动心得体会
2014/11/04 职场文书
2014年商场工作总结
2014/11/22 职场文书
Python爬虫基础之初次使用scrapy爬虫实例
2021/06/26 Python