dump a multi dimensional dictionary

C

cerr

Hi,

Can I somehow use pickle.dump() to store a dictionary of lists to a file?
I tried this:
import pickle
mylist = []
mydict = {}
mylist = '1','2'
mydict['3'] = mylist
fhg = open ("test", 'w')
pickle.dump(fhg,mydict)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/pickle.py", line 1370, in dump
Pickler(file, protocol).dump(obj)
File "/usr/lib/python2.7/pickle.py", line 203, in __init__
self.write = file.write
AttributeError: 'dict' object has no attribute 'write'{'3': ('1', '2')}

or should I just write my own dump function that can hanle thiS?

Please advise!

Thanks,
Ron
 
P

Prasad, Ramit

cerr said:
Hi,

Can I somehow use pickle.dump() to store a dictionary of lists to a file?
I tried this:
import pickle
mylist = []
mydict = {}
mylist = '1','2'
mydict['3'] = mylist
fhg = open ("test", 'w')
pickle.dump(fhg,mydict)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/pickle.py", line 1370, in dump
Pickler(file, protocol).dump(obj)
File "/usr/lib/python2.7/pickle.py", line 203, in __init__
self.write = file.write
AttributeError: 'dict' object has no attribute 'write'{'3': ('1', '2')}

or should I just write my own dump function that can hanle thiS?

Please advise!

Thanks,
Ron

I think you have the parameters for dump backwards.

According to API http://docs.python.org/2/library/pickle.html#pickle.dump
the format is: pickle.dump(obj, file, protocol=None)

Which means you need to use: pickle.dump(mydict, fhg)


Ramit



This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy andcompleteness of information, viruses, confidentiality, legal privilege, andlegal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email.
 
J

John Gordon

In said:
Can I somehow use pickle.dump() to store a dictionary of lists to a file?
I tried this:
import pickle
mylist = []
mydict = {}
mylist = '1','2'
mydict['3'] = mylist
fhg = open ("test", 'w')
pickle.dump(fhg,mydict)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/pickle.py", line 1370, in dump
Pickler(file, protocol).dump(obj)
File "/usr/lib/python2.7/pickle.py", line 203, in __init__
self.write = file.write
AttributeError: 'dict' object has no attribute 'write'{'3': ('1', '2')}
or should I just write my own dump function that can hanle thiS?

I think you have the arguments to pickle.dump() in the wrong order.
The data to be dumped should come first, then the file object.
 
S

Steven D'Aprano

or should I just write my own dump function that can hanle thiS?

Please advise!

Given the choice between reading the documentation to pickle.dump:

help(pickle.dump)


or spending the next month writing a replacement for pickle, testing it,
debugging it, going through revision after revision to try to bring it to
the same level of maturity as pickle, which would you prefer?

:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top