<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
Am 16.05.2010 09:10, schrieb Yutao Deng:
<blockquote
cite="mid:AANLkTil9Jyqlerf3XPFd63t5k4y5zMJe8jj_dYdvzO1g@mail.gmail.com"
type="cite">Hi all:<br>
I'm trying to learn to use Python wrote a applet to record every day
doing.<br>
and i use the pickle <br>
pickle.dump something to file no problem i think.<br>
but pickle.load whith a problem. can not load all dict do my way that
what i pickle.dump().<br>
<br>
My code:<br>
####<br>
import cPickle as pickle<br>
pickle_file = open("data2","rb")<br>
i = pickle.load(pickle_file)<br>
print i<br>
i = pickle.load(pickle_file)<br>
print i<br>
i = pickle.load(pickle_file)<br>
print i<br>
i = pickle.load(pickle_file)<br>
print i<br>
i = pickle.load(pickle_file)<br>
print i<br>
####<br>
console show :<br>
{'2010-5-23': ['1242', 'first']}<br>
{'2010-5-24': ['1232', 'third']}<br>
{'2010-5-25': ['211', 'second']}<br>
{'2010-3-22': ['3211', 'fourrrr']}<br>
{'2050-3-2': ['3990', '322']}<br>
<br>
This is i want but that's silly. if the dict too much, then i have not
ideas.<br>
<br>
the other way from <a moz-do-not-send="true"
href="http://mail.python.org/pipermail/tutor/2005-July/039859.html"
target="_blank">http://mail.python.org/pipermail/tutor/2005-July/039859.html</a><br>
<br>
####<br>
import cPickle as pickle<br>
pickle_file = open("data2","rb")<br>
<br>
number_of_pickles = pickle.load(pickle_file)<br>
for n in range(number_of_pickles):<br>
p = pickle.load(pickle_file)<br>
print p<br>
####<br>
this way didnt work for me.<br>
<br>
console show:<br>
Traceback (most recent call last):<br>
number_of_pickles = pickle.load(pickle_file)<br>
cPickle.UnpicklingError: invalid load key, '<br>
'.<br>
how do i define a range for pickle.load a file . i mean that how can i
know how many dict in the data2.
<pre wrap="">
<fieldset class="mimeAttachmentHeader"></fieldset>
_______________________________________________
Tutor maillist - <a class="moz-txt-link-abbreviated" href="mailto:Tutor@python.org">Tutor@python.org</a>
To unsubscribe or change subscription options:
<a class="moz-txt-link-freetext" href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</a>
</pre>
</blockquote>
If you want to unpickle a file which contains an unknown number of
pickled objects, this should work. You'll get a list containing all the
objects.<br>
#############################################################<br>
import cPickle<br>
import pprint<br>
<br>
test_data_1 = {"2010-12-13": ["1324", "first"]}<br>
test_data_2 = {"2010-12-14": ["234", "first_and_a_half"]}<br>
test_data_3 = {"2010-12-15": ["132224", "second"]}<br>
<br>
for data in (test_data_1, test_data_2, test_data_3):<br>
with open("data2.pickle", "ab") as file_stream:<br>
cPickle.dump(data, file_stream, 2)<br>
<br>
with open("data2.pickle", "rb") as file_stream:<br>
pickled_objects = []<br>
while True:<br>
try:<br>
pickled_objects.append(cPickle.load(file_stream))<br>
except EOFError:<br>
break<br>
<br>
print "Number of pickled objects is %s." % len(pickled_objects)<br>
pprint.pprint(pickled_objects)<br>
############################################################Cheers,<br>
<br>
Jan<br>
</body>
</html>