[Tutor] sensing EOF in Python 3.1

Cranky Frankie cranky.frankie at gmail.com
Wed Nov 23 19:03:22 CET 2011


From: Peter Otten <__peter__ at web.de> wrote:
snip
<<How did you write the data into the pickle file? The normal approach is to
write all your data in one step, e. g. (all code snippets untested)>>

Thanks Peter, that was it. I was treating pickle like standard file
i/o when it's not that at all.

The reason why I'm "pickling" is I'm trying to include information on
Python data structures in the presentaton I'm preparing.

Here are the two programs that now work correctly together:

import pickle
pickle_file = open("d:/Work/pickle_file", "wb")

Qb_dict = {"Montana": ["Joe", "Montana", "415-123-4567",
"joe.montana at gmail.com","Candlestick Park"],
"Tarkington": ["Fran", "651-321-7657", "frank.tarkington at gmail.com",
"Metropolitan Stadidum"],
"Namath": ["Joe", "212-222-7777", "joe.namath at gmail.com", "Shea Stadium"],
"Elway": ["John", "303-9876-333", "john.elway at gmai.com", "Mile High Stadium"],
"Elway": ["Ed", "303-9876-333", "john.elway at gmai.com", "Mile High
Stadium"],
"Manning": ["Archie","504-888-1234", "archie.manning at gmail.com",
"Louisiana Superdome"],
"Staubach": ["Roger","214-765-8989", "roger.staubach at gmail.com",
"Cowboy Stadium"]}

pickle.dump(Qb_dict, pickle_file)

pickle_file.close()



#
# pickle_in.py
# program to read in a pickled file
#
# Frank L. Palmeri
#

import pickle                                   # import the pickle
module
pickle_file = open("d:/Work/pickle_file", "rb") # open the pickled file

read_list = pickle.load(pickle_file)            # read the first pickled row

print(read_list)                            # print the input row from
the pickled file

pickle_file.close()                             # close the pickled file




-- 
Frank L. "Cranky Frankie" Palmeri


More information about the Tutor mailing list