Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51321

Re: dump a multi dimensional dictionary

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!xmission!newsswitch.lcs.mit.edu!bloom-beacon.mit.edu!panix!gordon
From John Gordon <gordon@panix.com>
Newsgroups comp.lang.python
Subject Re: dump a multi dimensional dictionary
Date Fri, 26 Jul 2013 21:00:24 +0000 (UTC)
Organization PANIX Public Access Internet and UNIX, NYC
Lines 32
Message-ID <ksuo18$g5u$1@reader1.panix.com> (permalink)
References <efdc6f6b-c061-4d3d-9d02-c397f8953d93@googlegroups.com>
NNTP-Posting-Host panix3.panix.com
X-Trace reader1.panix.com 1374872424 16574 166.84.1.3 (26 Jul 2013 21:00:24 GMT)
X-Complaints-To abuse@panix.com
NNTP-Posting-Date Fri, 26 Jul 2013 21:00:24 +0000 (UTC)
User-Agent nn/6.7.3
Xref csiph.com comp.lang.python:51321

Show key headers only | View raw


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"

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web