使用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通过exifread模块获得图片exif信息的方法
Mar 16 Python
python实现的简单窗口倒计时界面实例
May 05 Python
Python 的内置字符串方法小结
Mar 15 Python
Python实现的递归神经网络简单示例
Aug 11 Python
Python判断中文字符串是否相等的实例
Jul 06 Python
windows下搭建python scrapy爬虫框架步骤
Dec 23 Python
利用python求积分的实例
Jul 03 Python
python使用正则表达式(Regular Expression)方法超详细
Dec 30 Python
在tensorflow中实现屏蔽输出的log信息
Feb 04 Python
python模拟哔哩哔哩滑块登入验证的实现
Apr 24 Python
关于PySnooper 永远不要使用print进行调试的问题
Mar 04 Python
Python anaconda安装库命令详解
Oct 16 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版国家代码、缩写查询函数代码
2011/08/14 PHP
php递归获取目录内文件(包含子目录)封装类分享
2013/12/25 PHP
php教程之phpize使用方法
2014/02/12 PHP
php不写闭合标签的好处
2014/03/04 PHP
win7系统配置php+Apache+mysql环境的方法
2015/08/21 PHP
PHP读取文本文件并逐行输出该行使用最多的字符与对应次数的方法
2016/11/25 PHP
PHP面相对象中的重载与重写
2017/02/13 PHP
为Yahoo! UI Extensions Grid增加内置的可编辑器
2007/03/10 Javascript
jquery ready函数源代码研究
2009/12/06 Javascript
多种方法实现JS动态添加事件
2013/11/01 Javascript
使用jQuery重置(reset)表单的方法
2014/05/05 Javascript
关于延迟加载JavaScript
2015/05/05 Javascript
JavaScript中的anchor()方法使用详解
2015/06/08 Javascript
jQuery实用密码强度检测
2017/03/02 Javascript
jquery中封装函数传递当前元素的方法示例
2017/05/05 jQuery
VUE element-ui 写个复用Table组件的示例代码
2017/11/18 Javascript
vue组件通信传值操作示例
2019/01/08 Javascript
vue动画—通过钩子函数实现半场动画操作
2020/08/09 Javascript
[05:06]TI4西雅图DOTA2前线报道 海涛密探LGD训练
2014/07/09 DOTA
[02:55]2018DOTA2国际邀请赛勇士令状不朽珍藏Ⅲ饰品一览
2018/08/01 DOTA
python pandas实现excel转为html格式的方法
2018/10/23 Python
在Qt5和PyQt5中设置支持高分辨率屏幕自适应的方法
2019/06/18 Python
python实现websocket的客户端压力测试
2019/06/25 Python
解决pycharm同一目录下无法import其他文件
2020/02/12 Python
Python入门基础之数字字符串与列表
2021/02/01 Python
解决HTML5中的audio在手机端和微信端的不能自动播放问题
2019/11/04 HTML / CSS
SmartBuyGlasses美国官网:太阳眼镜和眼镜
2017/08/20 全球购物
美国孕妇装购物网站:Motherhood Maternity
2019/09/22 全球购物
医学专业毕业生个人的求职信
2013/12/04 职场文书
优秀毕业生自荐信范文
2014/01/01 职场文书
汽车运用工程专业求职信
2014/06/18 职场文书
关于环保的广播稿
2015/12/17 职场文书
MySQL获取所有分类的前N条记录
2021/05/07 MySQL
详解CSS伪元素的妙用单标签之美
2021/05/25 HTML / CSS
windows server 2012安装FTP并配置被动模式指定开放端口
2022/06/10 Servers
Python使用pandas导入csv文件内容的示例代码
2022/12/24 Python