Revisão | f2b96aabd502dccaa3318d52470959d220017cf1 (tree) |
---|---|
Hora | 2009-06-26 22:56:18 |
Autor | isella |
Commiter | isella |
I added a simple serialization example based on cpickle.
@@ -0,0 +1,36 @@ | ||
1 | +#!/usr/bin/env python | |
2 | +import cPickle | |
3 | +import scipy as s | |
4 | +inFridge = ["ketchup", "mustard", "relish"] | |
5 | +print inFridge | |
6 | +FILE = open("fridge.txt", 'w') | |
7 | +cPickle.dump(inFridge, FILE) | |
8 | +FILE.close() | |
9 | + | |
10 | +#Now I re-read the saved list into another format | |
11 | + | |
12 | +FILE2 = open("fridge.txt", 'r') | |
13 | +inFridgeFile = cPickle.load(FILE2) | |
14 | +FILE2.close() | |
15 | +print inFridgeFile | |
16 | + | |
17 | +#Bottom line: via cpickle I can actually store a list into | |
18 | +#a particular text file. | |
19 | + | |
20 | +#The same can be achieved for a numpy array | |
21 | + | |
22 | +z=s.arange(10)*s.sqrt(s.pi) | |
23 | + | |
24 | + | |
25 | +FILE = open("sci_arr.txt", 'w') | |
26 | +cPickle.dump(z, FILE) | |
27 | +FILE.close() | |
28 | + | |
29 | +FILE = open("sci_arr.txt", 'r') | |
30 | +z2 = cPickle.load(FILE) | |
31 | +FILE.close() | |
32 | +print z2 | |
33 | + | |
34 | + | |
35 | + | |
36 | +print "So far so good" |