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

 更新时间:2019年12月30日 17:51:58   作者:mygodhome  
今天小编就为大家分享一篇使用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)

以上这篇使用pickle存储数据dump 和 load实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • python机器学习之线性回归详解

    python机器学习之线性回归详解

    这篇文章主要介绍了python机器学习之线性回归详解,文中有非常详细的代码示例,对正在学习python的小伙伴们有很好的帮助,需要的朋友可以参考下
    2021-04-04
  • python实现xlsx文件分析详解

    python实现xlsx文件分析详解

    这篇文章主要为大家详细介绍了python实现xlsx文件分析,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • python根据给定文件返回文件名和扩展名的方法

    python根据给定文件返回文件名和扩展名的方法

    这篇文章主要介绍了python根据给定文件返回文件名和扩展名的方法,实例分析了Python操作文件及字符串的技巧,非常具有实用价值,需要的朋友可以参考下
    2015-03-03
  • python实现文件分组复制到不同目录的例子

    python实现文件分组复制到不同目录的例子

    这篇文章主要介绍了python实现文件按组复制到不同目录的例子,需要的朋友可以参考下
    2014-06-06
  • python对验证码降噪的实现示例代码

    python对验证码降噪的实现示例代码

    这篇文章主要介绍了python对验证码降噪的实现示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • Python selenium实现微博自动登录的示例代码

    Python selenium实现微博自动登录的示例代码

    本篇文章主要介绍了Python selenium实现微博自动登录的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • python中字符串的常见操作总结(一)

    python中字符串的常见操作总结(一)

    这篇文章主要介绍了python中字符串的常见操作总结,文章通过简单介绍对象的相关展开全文详细内容,需要的朋友可以参考一下
    2022-07-07
  • 用python画了个圣诞树给女朋友

    用python画了个圣诞树给女朋友

    大家好,本篇文章主要讲的是用python画了个圣诞树给女朋友,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2021-12-12
  • OpenCV视频流Python多线程处理方法详细分析

    OpenCV视频流Python多线程处理方法详细分析

    为OpenCV是搞计算机视觉必须要掌握的基础,这篇文章主要给大家介绍了关于OpenCV视频流多线程处理的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2022-11-11
  • 深入解析Python中的urllib2模块

    深入解析Python中的urllib2模块

    这篇文章主要介绍了Python中的urllib2模块,包括一个利用其抓取网站生成RSS的小例子,需要的朋友可以参考下
    2015-11-11

最新评论