Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #51320 > unrolled thread
| Started by | cerr <ron.eggler@gmail.com> |
|---|---|
| First post | 2013-07-26 13:21 -0700 |
| Last post | 2013-07-27 02:54 +0000 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.python
dump a multi dimensional dictionary cerr <ron.eggler@gmail.com> - 2013-07-26 13:21 -0700
Re: dump a multi dimensional dictionary John Gordon <gordon@panix.com> - 2013-07-26 21:00 +0000
RE: dump a multi dimensional dictionary "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2013-07-26 20:40 +0000
Re: dump a multi dimensional dictionary Chris Angelico <rosuav@gmail.com> - 2013-07-27 00:33 +0100
Re: dump a multi dimensional dictionary Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-27 02:54 +0000
| From | cerr <ron.eggler@gmail.com> |
|---|---|
| Date | 2013-07-26 13:21 -0700 |
| Subject | dump a multi dimensional dictionary |
| Message-ID | <efdc6f6b-c061-4d3d-9d02-c397f8953d93@googlegroups.com> |
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'
>>> print mydict
{'3': ('1', '2')}
or should I just write my own dump function that can hanle thiS?
Please advise!
Thanks,
Ron
[toc] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2013-07-26 21:00 +0000 |
| Message-ID | <ksuo18$g5u$1@reader1.panix.com> |
| In reply to | #51320 |
In <efdc6f6b-c061-4d3d-9d02-c397f8953d93@googlegroups.com> cerr <ron.eggler@gmail.com> writes:
> 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'
> >>> print mydict
> {'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.
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | "Prasad, Ramit" <ramit.prasad@jpmorgan.com> |
|---|---|
| Date | 2013-07-26 20:40 +0000 |
| Message-ID | <mailman.5156.1374874858.3114.python-list@python.org> |
| In reply to | #51320 |
cerr wrote:
> 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'
> >>> print mydict
> {'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 and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-07-27 00:33 +0100 |
| Message-ID | <mailman.5159.1374881646.3114.python-list@python.org> |
| In reply to | #51320 |
On Fri, Jul 26, 2013 at 9:21 PM, cerr <ron.eggler@gmail.com> wrote:
> >>> mylist = []
> >>> mydict = {}
> >>> mylist = '1','2'
Side point: mylist is no longer a list, it's a tuple. I don't think
pickle has problems with tuples, but it's worth noting that
difference.
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-07-27 02:54 +0000 |
| Message-ID | <51f33663$0$29971$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #51320 |
On Fri, 26 Jul 2013 13:21:47 -0700, cerr wrote: > 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? :-) -- Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web