Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

Pickleing

Question posted by: sirdeltalot (Newbie) on June 29th, 2008 04:37 PM
Hi
I'm a total beginner in python.
Want i want to know is how to pickle a dictionary after a user has updated it then read back from the pickled file.
Thanks
Iz
bvdet's Avatar
bvdet
Expert
1,147 Posts
June 29th, 2008
10:28 PM
#2

Re: Pickleing
I used to use the following functions for saving and recalling a dictionary or list of dictionaries to a file using pickle.
Expand|Select|Wrap|Line Numbers
  1. import pickle
  2.  
  3. def import_data(file_name):
  4.     try:
  5.         f = open(file_name, "r")
  6.     except IOError, e:
  7.         # unable to open file
  8.         Warning("Default file import error. Reverting to original defaults...")
  9.         return None
  10.  
  11.     # file open is successful
  12.     try:
  13.  
  14.         dd = pickle.Unpickler(f).load()
  15.         f.close()
  16.         # test for a dictionary or list of dictionaries
  17.         if isinstance(dd, dict):
  18.             return dd
  19.         elif isinstance(dd, list):
  20.             for i, item in enumerate(dd):
  21.                 if not isinstance(item, dict):
  22.                     Warning("**INVALID** List item %s is not a dictionary." % (i))
  23.                     return None
  24.             # always return one dictionary
  25.             ddr = {}
  26.             for d in dd: ddr.update(d)
  27.             return ddr
  28.         else:
  29.             # dd is not a dictionary or list of dictionaries
  30.             Warning("Invalid imported data type.\nData must be a dictionary or list of dictionaries.")
  31.             return None
  32.  
  33.     except:
  34.         # file is not compatible with Unpickler - close file and warn user
  35.         f.close()
  36.         Warning("Invalid defaults file. Reverting to original defaults...")
  37.         return None
  38.  
  39. def export_data(file_name, dd):
  40.     if check_Defaults_dir(os.path.dirname(file_name)):
  41.         # directory exists or was created
  42.         try:
  43.             f = open(file_name, "w")
  44.             pickle.Pickler(f).dump(dd)
  45.             f.close()
  46.             return True
  47.         except:
  48.             Warning("Default values file export error.")
  49.             return False
  50.  
  51.     else:
  52.         # directory did not exist and the user chose 'no' to create directory
  53.         Warning("The default values file export was aborted")
  54.         return False

Reply
sirdeltalot's Avatar
sirdeltalot
Newbie
7 Posts
July 2nd, 2008
06:24 AM
#3

Re: Pickleing
That's realy helpful,
Thanks!

Reply
Reply
Not the answer you were looking for? Post your question . . .
189,798 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top Python Forum Contributors