使用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" (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"htmlcode">with open('new_man.txt','w') as new_man_file: nester.print_lol(new_man,fn=new_man_file)以上这篇使用pickle存储数据dump 和 load实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“使用pickle存储数据dump 和 load实例讲解”评论...
更新动态
2024年11月25日
2024年11月25日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]