使用pickle存储数据dump 和 load实例讲解
作者:mygodhome 时间:2023-05-19 18:50:18
使用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
标签:pickle,存储数据,dump,load
0
投稿
猜你喜欢
详解MySQL 数据库优化方法
2010-08-12 14:50:00
numpy之多维数组的创建全过程
2023-06-22 03:58:03
基于Python实现的微信好友数据分析
2021-07-26 20:27:54
Pycharm2020最新激活码|永久激活(附最新激活码和插件的详细教程)
2023-11-16 07:44:20
Javascript中作用域的详细介绍
2024-04-18 10:02:09
SQL事务用法begin tran,commit tran和rollback tran的用法
2012-01-05 18:58:51
python热力图实现简单方法
2023-10-28 06:14:41
数据库访问性能优化
2024-01-21 18:24:47
内容页页码的预览导航
2008-07-07 16:43:00
Python+selenium 获取一组元素属性值的实例
2021-06-06 02:28:27
如何显示数据库里的图片?
2010-06-08 09:36:00
Vue便签的简单实现
2024-05-02 17:03:31
ThinkPHP框架实现用户信息查询更新及删除功能示例
2024-06-07 15:34:11
ASP.NET 2.0中Gridview控件高级技巧图文教程
2007-08-07 15:46:00
JavaScript代码执行的先后顺序问题
2024-04-29 13:45:54
防注入asp过滤sql特殊字符函数
2007-10-23 17:50:00
True or False,明明白白你的If语句流程
2008-01-25 19:00:00
Python快速查找list中相同部分的方法
2021-01-28 17:26:00
python3读取autocad图形文件.py实例
2022-03-11 09:08:45
对TensorFlow的assign赋值用法详解
2023-03-18 22:52:56