I need to know how to use pickle (and its brethren) correctly. Does anyone know of a good tutorial? The python manual is a bit too dense on this subject.
8 5147
I need to know how to use pickle (and its brethren) correctly. Does anyone know of a good tutorial? The python manual is a bit too dense on this subject.
It's really quite simple: -
>>> import cPickle
-
>>> strList = ['asf','dadf','adf','adf',]
-
>>> pickleFile = open("pickleTest.dat", "w")
-
>>> cPickle.dump(strList, pickleFile)
-
>>> pickleFile.close()
-
>>> pickleFile = open("pickleTest.dat")
-
>>> unpickledList = cPickle.load(pickleFile)
-
>>> print unpickledList
-
['asf', 'dadf', 'adf', 'adf']
-
>>> pickleFile.close()
-
>>>
how about this ?here
Great Post! I'll put that link into the new sticky (actually the parent page). Thanks gd
No prob :)
two posts to go...
bvdet 2,851
Expert Mod 2GB
I need to know how to use pickle (and its brethren) correctly. Does anyone know of a good tutorial? The python manual is a bit too dense on this subject.
We use Pickler and Unpickler in several of our applications to save and restore a dictionary or list of dictionaries.
Library functions: - import os
-
import macrolib.pickle
-
from param import Warning
-
-
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 = macrolib.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")
-
macrolib.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
Typical function calls: - ## auto import defaults data if enabled
-
if enable_default_import_export == "Enable":
-
dd0 = import_data(os.path.join(default_file_path, def_file))
-
if dd0:
-
for key, value in dd0.items():
-
exec "%s = %s" % (key, repr(value)) in None
-
else:
-
Warning("Invalid data - Reverting to original defaults")
-
## auto export default values to disk if enabled
-
if enable_default_import_export == "Enable":
-
export_data(os.path.join(default_file_path, def_file), dd_list)
Module pickle works with many data types. From Python Essential Reference:
"Multiple calls to the dump() and load() methods are allowed, provided that the sequence of load() calls used to restore a collection of previously stored objects matches the sequence of dump() calls during the pickling process."
HTH :-),
BV
I figured out how to do what I wanted to do (before you guys posted :p). But I'll have a look at these.
I figured out how to do what I wanted to do (before you guys posted :p). But I'll have a look at these.
Did you notice that the docs say that cPickle can be 1000 times faster than the old pickle module?
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Simon Burton |
last post by:
Hi,
I am pickling big graphs of data and running into this problem:
File "/usr/lib/python2.2/pickle.py", line 225, in save
f(self, object)
File "/usr/lib/python2.2/pickle.py", line 414, in...
|
by: Michael Hohn |
last post by:
Hi,
under python 2.2, the pickle/unpickle sequence incorrectly restores
a larger data structure I have.
Under Python 2.3, these structures now give an explicit exception from...
|
by: Grant Edwards |
last post by:
I finally figured out why one of my apps sometimes fails under
Win32 when it always works fine under Linux: Under Win32, the
pickle module only works with a subset of floating point
values. In...
|
by: Shi Mu |
last post by:
I got a sample code and tested it but really can not understand the
use of pickle and dump:
>>> import pickle
>>> f = open("try.txt", "w")
>>> pickle.dump(3.14, f)
>>> pickle.dump(, f)
>>>...
|
by: Jim Lewis |
last post by:
Pickling an instance of a class, gives "can't pickle instancemethod
objects". What does this mean? How do I find the class method creating
the problem?
|
by: Chris |
last post by:
Why can pickle serialize references to functions, but not methods?
Pickling a function serializes the function name, but pickling a
staticmethod, classmethod, or instancemethod generates an...
|
by: Nagu |
last post by:
I am trying to save a dictionary of size 65000X50 to a local file and
I get the memory error problem.
How do I go about resolving this? Is there way to partition the pickle
object and combine...
|
by: Nagu |
last post by:
I am trying to save a dictionary of size 65000X50 to a local file and
I get the memory error problem.
How do I go about resolving this? Is there way to partition the pickle
object and combine...
|
by: IceMan85 |
last post by:
Hi to all, I have spent the whole morning trying, with no success to pickle an object that I have created.
The error that I get is : Can't pickle 'SRE_Match' object: <_sre.SRE_Match object at...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |