码迷,mamicode.com
首页 > 编程语言 > 详细

[Head First Python]4. pickle.dump pickle.load

时间:2014-06-28 23:02:43      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   文件   数据   os   

sketch.py

 1 #--*-- coding:utf-8 --*--
 2 import pickle
 3 import nester
 4 man = []
 5 other = []
 6 try:
 7     data = open ("datafile.txt")
 8 
 9     for each_line in data:
10         try:    
11             (role, line_spoken) = each_line.split(":", 1)
12             line_spoken = line_spoken.strip()
13             if role == Man:
14                 man.append(line_spoken)
15             elif role == Other Man:
16                 other.append(line_spoken)    
17             
18         except ValueError:
19             pass
20 
21     data.close()
22 except IOError:
23     print(this data file is missing!)
24 
25 try:
26     with open(man.out, wb) as man_out, open(other.out,wb) as other_out:
27         pickle.dump(man, man_out) #二进制存储数据到文件
28         pickle.dump(other, other_out)
29 
30 except pickle.PickleError as perr:
31     print(pickle error + str(perr))
32 
33 new_man = []
34 try:
35     with open(man.out, rb) as man_file:
36         new_man = pickle.load(man_file) #二进制文件数据读取到list
37 except IOError as err:
38     print(file error + str(err))
39 except pickle.PickleError as perr:
40     print(pickle err + str(perr))
41 
42 nester.print_lol(new_man)

 

[Head First Python]4. pickle.dump pickle.load,布布扣,bubuko.com

[Head First Python]4. pickle.dump pickle.load

标签:style   blog   color   文件   数据   os   

原文地址:http://www.cnblogs.com/galoishelley/p/3794200.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!