使用pickle存储数据dump 和 load

本文介绍如何利用Python的pickle模块将文本文件中的数据序列化并存储为二进制文件,之后再将这些二进制文件反序列化,还原为原始数据。通过示例展示了从文本文件读取数据、使用pickle进行存储以及读取的过程。

使用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)



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值