Re: [Tutor] pickle.load() all dict

2010-05-16 Thread Yutao
Thank you Jan.
you are right . an exception whitout a reason not conducive to pragramming.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] updating python on a mac

2010-05-16 Thread Alan Gauld
"Karl Jansson"  wrote 

I downloaded python 3.1.2 for the mac, and I don't know how to make it  
so that when I type "python" in the terminal that I get python 3.1.2  
instead of 2.5.1, which came with my mac.


I haven't done this personally so I can only guess but...


/Applications/Python\ 3.1/Update\ Shell\ Profile.command ; exit;
This script will update your shell profile when the 'bin' directory
of python is not early enough of the PATH of your shell.
These changes will be effective only in shell windows that you open
after running this script.
All right, you're a python lover already
logout


Did you follow these instructions? ie did you logout and then restart 
the shell window - ie Terminal?


If so and it didn't work try running the env command

Look for the PATH entry and see whether Python 2.5 comes before Python 3.1.

If so you probably need to edit your ~/.profile or ~/.bashrc or ~/.cshrc 
depending which shell you use.


HTH

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] updating python on a mac

2010-05-16 Thread Karl Jansson
I downloaded python 3.1.2 for the mac, and I don't know how to make it  
so that when I type "python" in the terminal that I get python 3.1.2  
instead of 2.5.1, which came with my mac.


In the instructions, it says to "update shell profile" but when I do  
that I get the following message, and nothing gets updated:




/Applications/Python\ 3.1/Update\ Shell\ Profile.command ; exit;
This script will update your shell profile when the 'bin' directory
of python is not early enough of the PATH of your shell.
These changes will be effective only in shell windows that you open
after running this script.
All right, you're a python lover already
logout

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pickle.load() all dict

2010-05-16 Thread Knacktus

Am 16.05.2010 14:45, schrieb Yutao Deng:

LOL, try...except is a good idea.
i fix it  lick this:


with open("data2.pickle","rb") as file_stream:
c = 0
while True:
try:
i = cPickle.load(file_stream)
print i
c += 1
except:
print "Numer of pickled objects is %s." %c
break
#


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
   
Just a remark on your except statement: You should not use "except:" 
without specifying a concrete exception class, because all kind of 
exceptions are then handled by the except block (and therefore hidden). 
If for example an IOError occurs while reading the file (for what ever 
reason...), it would be handled by your except block and you would never 
know that there was a problem reading the file. If you use "except 
EOFError:" the exception block is evaluated only when the EOFError 
occurs. All other exceptions are raised as usual.


Best regards,

Jan
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pickle.load() all dict

2010-05-16 Thread Yutao Deng
LOL, try...except is a good idea.
i fix it  lick this:


with open("data2.pickle","rb") as file_stream:
c = 0
while True:
try:
i = cPickle.load(file_stream)
print i
c += 1
except:
print "Numer of pickled objects is %s." %c
break
#
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pickle.load() all dict

2010-05-16 Thread Knacktus

Am 16.05.2010 09:10, schrieb Yutao Deng:

Hi all:
I'm trying to learn to use Python  wrote a applet to record every day 
doing.

and i use the pickle
pickle.dump something to file no problem i think.
but pickle.load whith a problem. can not load all dict do my way that 
what i pickle.dump().


My code:

import cPickle as pickle
pickle_file = open("data2","rb")
i = pickle.load(pickle_file)
print i
i = pickle.load(pickle_file)
print i
i = pickle.load(pickle_file)
print i
i = pickle.load(pickle_file)
print i
i = pickle.load(pickle_file)
print i

console show :
{'2010-5-23': ['1242', 'first']}
{'2010-5-24': ['1232', 'third']}
{'2010-5-25': ['211', 'second']}
{'2010-3-22': ['3211', 'fou']}
{'2050-3-2': ['3990', '322']}

This is i want but that's silly. if the dict too much, then i have not 
ideas.


the other way from 
http://mail.python.org/pipermail/tutor/2005-July/039859.html



import cPickle as pickle
pickle_file = open("data2","rb")

number_of_pickles = pickle.load(pickle_file)
for n in range(number_of_pickles):
p = pickle.load(pickle_file)
print p

this way didnt work for me.

console show:
Traceback (most recent call last):
number_of_pickles = pickle.load(pickle_file)
cPickle.UnpicklingError: invalid load key, '
'.
how do i define a range for pickle.load a file . i mean that how can i 
know how many dict in the data2.



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
   
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.

#
import cPickle
import pprint

test_data_1 = {"2010-12-13": ["1324", "first"]}
test_data_2 = {"2010-12-14": ["234", "first_and_a_half"]}
test_data_3 = {"2010-12-15": ["132224", "second"]}

for data in (test_data_1, test_data_2, test_data_3):
with open("data2.pickle", "ab") as file_stream:
cPickle.dump(data, file_stream, 2)

with open("data2.pickle", "rb") as file_stream:
pickled_objects = []
while True:
try:
pickled_objects.append(cPickle.load(file_stream))
except EOFError:
break

print "Number of pickled objects is %s." % len(pickled_objects)
pprint.pprint(pickled_objects)
Cheers,

Jan
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] pickle.load() all dict

2010-05-16 Thread Yutao Deng
Hi all:
I'm trying to learn to use Python  wrote a applet to record every day doing.
and i use the pickle
pickle.dump something to file no problem i think.
but pickle.load whith a problem. can not load all dict do my way that what i
pickle.dump().

My code:

import cPickle as pickle
pickle_file = open("data2","rb")
i = pickle.load(pickle_file)
print i
i = pickle.load(pickle_file)
print i
i = pickle.load(pickle_file)
print i
i = pickle.load(pickle_file)
print i
i = pickle.load(pickle_file)
print i

console show :
{'2010-5-23': ['1242', 'first']}
{'2010-5-24': ['1232', 'third']}
{'2010-5-25': ['211', 'second']}
{'2010-3-22': ['3211', 'fou']}
{'2050-3-2': ['3990', '322']}

This is i want but that's silly. if the dict too much, then i have not
ideas.

the other way from
http://mail.python.org/pipermail/tutor/2005-July/039859.html


import cPickle as pickle
pickle_file = open("data2","rb")

number_of_pickles = pickle.load(pickle_file)
for n in range(number_of_pickles):
p = pickle.load(pickle_file)
print p

this way didnt work for me.

console show:
Traceback (most recent call last):
number_of_pickles = pickle.load(pickle_file)
cPickle.UnpicklingError: invalid load key, '
'.
how do i define a range for pickle.load a file . i mean that how can i know
how many dict in the data2.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor