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
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. - import pickle
-
-
def import_data(file_name):
-
try:
-
f = open(file_name, "r")
-
except IOError, e:
-
# unable to open file
-
Warning("Default file import error. Reverting to original defaults...")
-
return None
-
-
# file open is successful
-
try:
-
-
dd = pickle.Unpickler(f).load()
-
f.close()
-
# test for a dictionary or list of dictionaries
-
if isinstance(dd, dict):
-
return dd
-
elif isinstance(dd, list):
-
for i, item in enumerate(dd):
-
if not isinstance(item, dict):
-
Warning("**INVALID** List item %s is not a dictionary." % (i))
-
return None
-
# always return one dictionary
-
ddr = {}
-
for d in dd: ddr.update(d)
-
return ddr
-
else:
-
# dd is not a dictionary or list of dictionaries
-
Warning("Invalid imported data type.\nData must be a dictionary or list of dictionaries.")
-
return None
-
-
except:
-
# file is not compatible with Unpickler - close file and warn user
-
f.close()
-
Warning("Invalid defaults file. Reverting to original defaults...")
-
return None
-
-
def export_data(file_name, dd):
-
if check_Defaults_dir(os.path.dirname(file_name)):
-
# directory exists or was created
-
try:
-
f = open(file_name, "w")
-
pickle.Pickler(f).dump(dd)
-
f.close()
-
return True
-
except:
-
Warning("Default values file export error.")
-
return False
-
-
else:
-
# directory did not exist and the user chose 'no' to create directory
-
Warning("The default values file export was aborted")
-
return False
That's realy helpful,
Thanks!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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,...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
| |