使用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环境变量设置方法
Aug 28 Python
使用Python的package机制如何简化utils包设计详解
Dec 11 Python
python写一个md5解密器示例
Feb 23 Python
Python处理中文标点符号大集合
May 14 Python
python抓取网站的图片并下载到本地的方法
May 22 Python
Python使用pyserial进行串口通信的实例
Jul 02 Python
用Python实现将一张图片分成9宫格的示例
Jul 05 Python
Django使用 Bootstrap 样式修改书籍列表过程解析
Aug 09 Python
Python Web框架之Django框架Model基础详解
Aug 16 Python
tensorflow没有output结点,存储成pb文件的例子
Jan 04 Python
Python批量安装卸载1000个apk的方法
Apr 10 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提交后跳转
2013/06/23 PHP
CodeIgniter框架提示Disallowed Key Characters的解决办法
2014/04/21 PHP
Laravel路由设定和子路由设定实例分析
2016/03/30 PHP
PHP实现通过URL提取根域名
2016/03/31 PHP
PHP使用token防止表单重复提交的方法
2016/04/07 PHP
php分页查询mysql结果的base64处理方法示例
2017/05/18 PHP
golang实现php里的serialize()和unserialize()序列和反序列方法详解
2018/10/30 PHP
laravel-admin表单提交隐藏一些数据,回调时获取数据的方法
2019/10/08 PHP
juqery 学习之五 文档处理 插入
2011/02/11 Javascript
JavaScript 继承使用分析
2011/05/12 Javascript
js使用正则实现ReplaceAll全部替换的方法
2014/07/18 Javascript
javascript如何实现暂停功能
2015/11/06 Javascript
详解JavaScript中的Unescape()和String() 函数
2015/11/09 Javascript
分享自己用JS做的扫雷小游戏
2016/02/17 Javascript
JS实现点击登录弹出窗口同时背景色渐变动画效果
2016/03/25 Javascript
jQuery的ready方法实现原理分析
2016/10/26 Javascript
JS实现淡入淡出图片效果的方法分析
2016/12/20 Javascript
bootstrap中添加额外的图标实例代码
2017/02/15 Javascript
JQuery的加载和选择器用法简单示例
2019/05/13 jQuery
JS求解两数之和算法详解
2020/04/28 Javascript
解决antd datepicker 获取时间默认少8个小时的问题
2020/10/29 Javascript
python web基础之加载静态文件实例
2018/03/20 Python
Python中py文件引用另一个py文件变量的方法
2018/04/29 Python
python中enumerate() 与zip()函数的使用比较实例分析
2019/09/03 Python
python绘制随机网络图形示例
2019/11/21 Python
利用Python计算KS的实例详解
2020/03/03 Python
Python:__eq__和__str__函数的使用示例
2020/09/26 Python
python 实现一个简单的线性回归案例
2020/12/17 Python
比利时网上药店: Drogisterij.net
2017/03/17 全球购物
菲律宾购物网站:Lazada菲律宾
2018/04/05 全球购物
瑞士隐形眼镜和护理产品网上商店:Linsenklick
2019/10/21 全球购物
爱尔兰橄榄球店:Irish Rugby Store
2019/12/05 全球购物
总经理秘书工作职责
2013/12/26 职场文书
表演方阵解说词
2014/02/08 职场文书
调解协议书范本
2016/03/21 职场文书
《思路决定出路》读后感3篇
2019/12/11 职场文书