使用pickle存储数据dump 和 load
来源:互联网 发布:js object 添加元素 编辑:程序博客网 时间:2024/04/30 20:07
使用pickle模块来dump你的数据:对上篇博客里的sketch.txt文件:
import osimport sysimport pickleman=[ ]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!qX Oh no you didn't!qX Oh look, this isn't an argument!qX No it isn't!qX It's just contradiction!qX 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 nesterimport osimport sysimport pickleman=[ ]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)
0 0
- 使用pickle存储数据dump 和 load
- Python 3.6 json和pickle的dump和load方法
- Python pickle 的 dump() & load()
- python pickle : dump , load variable into a file
- python数据持久存储:pickle模块的基本使用
- python数据持久存储:pickle模块的基本使用
- python数据持久存储:pickle模块的基本使用
- python数据持久存储:pickle模块的基本使用
- python数据持久存储:pickle模块的基本使用
- python数据持久存储:pickle模块的基本使用
- python数据持久存储:pickle模块的基本使用
- python数据持久存储:pickle模块的基本使用
- Python数据存储:pickle模块的使用讲解
- python数据持久存储:pickle模块的基本使用
- python数据持久存储:pickle模块的基本使用
- python数据持久存储:pickle模块的基本使用
- python数据持久存储:pickle模块的基本使用
- python数据持久存储:pickle模块的基本使用
- 实体类(VO,DO,DTO)的划分
- android数据库框架LitePal的简单使用
- 根据IP地址十进制范围--获得IP地址所在地
- T语言与快速开发平台之天宇开发平台开发指南
- 为什么需要把应用中出现的文字单独存放在string.xml文件中
- 使用pickle存储数据dump 和 load
- activemq 中的 concurrentConsumers
- CString转为wchar_t
- DELL服务器装上oracle 11G怎么修改SCAN IP讲解
- 在WPF里面按Enter键得到Tab的效果
- CSS-响应式框架
- 深入理解PHP Opcode缓存原理
- 用几何画板画矩形网格的技巧
- ssm框架的简单搭建