473,388 Members | 1,376 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Pickleing

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
Jun 29 '08 #1
2 1075
bvdet
2,851 Expert Mod 2GB
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
Jun 29 '08 #2
That's realy helpful,
Thanks!
Jul 2 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Gregor Horvath | last post by:
Hi, given the dynamic nature of python I assume that there is an elegant solution for my problem, but I did not manage to find it. I have a file that contains for example on line: when...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.