使用pickle存储数据dump 和 load实例讲解

yipeiwu_com5年前Python基础

使用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设计】。

相关文章

Pyqt5如何让QMessageBox按钮显示中文示例代码

Pyqt5如何让QMessageBox按钮显示中文示例代码

前言 QMessageBox是一种通用的弹出框对话框;包含:提示、警告、错误、咨询、关于等对话框;只是显示图标不同,其他功能类似; QMessageBox类常用方法如下: 按钮类型:...

Python中Selenium模拟JQuery滑动解锁实例

Python中Selenium模拟JQuery滑动解锁实例

本文介绍了Python中Selenium模拟JQuery滑动解锁实例,分享给大家,也给自己留个笔记 滑动解锁一直做UI自动化的难点之一,我补一篇滑动解锁的例子,希望能给初做Web UI自...

Python找出list中最常出现元素的方法

本文实例讲述了Python找出list中最常出现元素的方法。分享给大家供大家参考,具体如下: 假设一个list中保存着各种元素,需要统计每个元素出现的个数,并打印出最常出现的前三个元素分...

Python程序员鲜为人知但你应该知道的17个问题

一、不要使用可变对象作为函数默认值复制代码 代码如下:In [1]: def append_to_list(value, def_list=[]):   ...:&n...

使用Python进行防病毒免杀解析

使用Python进行防病毒免杀解析

很多渗透工具都提供了权限维持的能力,如Metasploit、Empire和Cobalt Strike,但是都会被防病毒软件检测到这种恶意行为。在探讨一个权限维持技巧的时候,似乎越来越多的...