使用pickle存储数据dump 和 load实例讲解
作者:mygodhome 发布时间:2023-05-19 18:50:18
标签:pickle,存储数据,dump,load
使用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)
来源:https://blog.csdn.net/mygodhome/article/details/53418577


猜你喜欢
- 前言前端小伙伴们平常在开发过程中文件上传是经常遇到的一个问题,也许你能够实现相关的功能,但是做完后回想代码实现上是不是有点"力不从
- 物化表首先提出一个不相关的IN子查询SELECT * FROM s1 WHERE key1 IN (SELECT common_field
- 其实很简单,用len函数:>>> array = [0,1,2,3,4,5] >>> print len
- 研究好多天了,也试过好多办法了,总结出目前发现最好的方法:先说一下基本的东西:<%@ codepage=65001%>
- 本文分享的实例主要实现的是Python+matplotlib绘制一个有阴影和没有阴影的3D条形图,具体如下。首先看看演示效果:完整代码如下:
- 电脑安装git客户端、注册github账号并登陆到本地项目文件夹右键选择git bash here输入个人信息(代码提交者)git conf
- mysql支持很多字段类型,包括数值类型、日期/时间类型和字符串(字符)类型;在使用时需要考虑到存储空间,存储效率;几种列类型描述使用了下述
- 现像如下:站点无法打开,或者打开很慢.HTML可以打开.重新启动或者回收应用程序池可恢复.但过一段时间又会出现日志里会有:ISAPI
- 1.python实现对doc文档的读取#读取docx中的文本代码示例import docx#获取文档对象file=docx.Document
- CSS 文件的大小和所引起的 HTTP 的请求数是 CSS 性能的最关键因素回流(reflow)和渲染时间(非常!)没那么重要副本(dupl
- 这篇文章主要介绍了Python遍历字典方式就实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友
- gettext 是GNU 提供的一套 国际化与本地化 处理的相关函数库。大多数语言都有对应的gettext实现。本文主要使用jed 来实现g
- 我就废话不多说了,还是直接看代码吧!# 利用python在内存中读写str和二进制数据from io import StringIOfrom
- TensorFlow-gpu1.安装Anaconda进入官网(https://www.anaconda.com/) ->get sta
- 看到网上也有开源的代码,这不,我拿来进行了二次重写,呵呵,上代码: #encoding: utf-8&n
- 如果MySQL服务器启用了二进制日志,你可以使用mysqlbinlog工具来恢复从指定的时间点开始 (例如,从你最后一次备份)直到现在或另一
- 使用explain关键字可以模拟优化器执行SQL查询语句,从而知道MySQL是如何处理你的SQL语句的,分析你的查询语句或是表结构的性能瓶颈
- xbox series和ps5发售以来,国内黄牛价格一直居高不下。虽然海外amazon上ps5补货很少而且基本撑不过一分钟,但是xbox s
- 先用使用常规方法,两个指针:golang实现:type Node struct { value int next *
- MySQL中group_concat函数,完整的语法如下:group_concat([DISTINCT] 要连接的字段 [Order BY