使用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基于回溯法子集树模板实现8皇后问题
Sep 01 Python
PyQt5利用QPainter绘制各种图形的实例
Oct 19 Python
Sanic框架安装与简单入门示例
Jul 16 Python
python中多个装饰器的执行顺序详解
Oct 08 Python
python3.6环境安装+pip环境配置教程图文详解
Jun 20 Python
Python替换月份为英文缩写的实现方法
Jul 15 Python
Django 重写用户模型的实现
Jul 29 Python
在python3中实现更新界面
Feb 21 Python
Python切割图片成九宫格的示例代码
Mar 10 Python
PyQt中使用QtSql连接MySql数据库的方法
Jul 28 Python
Django-silk性能测试工具安装及使用解析
Nov 28 Python
python调用ffmpeg命令行工具便捷操作视频示例实现过程
Nov 01 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/03/08 PHP
基于python发送邮件的乱码问题的解决办法
2013/04/25 PHP
如何批量清理系统临时文件(语言:C#、 C/C++、 php 、python 、java )
2016/02/01 PHP
PHP仿微信发红包领红包效果
2016/10/30 PHP
javascript Zifa FormValid 0.1表单验证 代码打包下载
2007/06/08 Javascript
JavaScript isPrototypeOf和hasOwnProperty使用区别
2010/03/04 Javascript
关于flash遮盖div浮动层的解决方法
2010/07/17 Javascript
JQuery用户名校验的具体实现
2016/03/18 Javascript
JS在一定时间内跳转页面及各种刷新页面的实现方法
2016/05/26 Javascript
总结javascript中的六种迭代器
2016/08/16 Javascript
VUE基于NUXT的SSR 服务端渲染
2018/11/30 Javascript
详解package.json版本号规则
2019/08/01 Javascript
vue实现节点增删改功能
2019/09/26 Javascript
VUE中setTimeout和setInterval自动销毁案例
2020/09/07 Javascript
js实现电灯开关效果
2021/01/19 Javascript
[54:28]EG vs OG 2019国际邀请赛小组赛 BO2 第一场 8.16
2019/08/18 DOTA
Python读取键盘输入的2种方法
2015/06/16 Python
python抓取文件夹的所有文件
2018/02/27 Python
浅谈django rest jwt vue 跨域问题
2018/10/26 Python
python实现可变变量名方法详解
2019/07/01 Python
pandas删除行删除列增加行增加列的实现
2019/07/06 Python
Python 批量刷博客园访问量脚本过程解析
2019/08/30 Python
python获取Linux发行版名称
2019/08/30 Python
解决Tensorflow占用GPU显存问题
2020/02/03 Python
基于TensorBoard中graph模块图结构分析
2020/02/15 Python
Coggles美国/加拿大:高级国际时装零售商
2018/10/23 全球购物
Footshop法国:购买运动鞋
2020/01/19 全球购物
美国球迷装备的第一来源:FOCO
2020/07/03 全球购物
医学生实习自荐信
2013/10/01 职场文书
十月份红领巾广播稿
2014/01/22 职场文书
计算机学生的自我评价分享
2014/02/18 职场文书
2014年精神文明工作总结
2014/12/23 职场文书
2019个人年度目标制定攻略!
2019/07/12 职场文书
MySQL表字段时间设置默认值
2021/05/13 MySQL
直播实况, OMG破敌三路五十分钟大战神技局摩托车
2022/04/01 DOTA
SQL Server使用CROSS APPLY与OUTER APPLY实现连接查询
2022/05/25 SQL Server