Re: save dictionary for later use?



globalrev wrote:

pickle.dumps(mg)
pickle.load(mg)

'dict' object has no attribute 'readline'
dumps load(well i dont know but i get no complaint but running load
generates that error)

The 'loads' and 'dumps' methods use strings:

>>> import pickle
>>> d = {"this": 42, "that": 101, "other": 17}
>>> s = pickle.dumps(d)
>>> s
"(dp0\nS'this'\np1\nI42\nsS'other'\np2\nI17\nsS'that'\np3\nI101\ns."
>>> pickle.loads(s)
{'this': 42, 'other': 17, 'that': 101}

If you want to store to / restore from file, use 'dump' and 'load':

# write to file 'out'...
>>> f = open("out")
>>> f = open("out", "wb")
>>> pickle.dump(d, f)
>>> f.close()

# restore it later
>>> g = open("out", "rb")
>>> e = pickle.load(g)
>>> g.close()
>>> e
{'this': 42, 'other': 17, 'that': 101}

Also see http://docs.python.org/lib/pickle-example.html.

Hope this helps!

--Hans
.



Relevant Pages

  • Re: save dictionary for later use?
    ... dumps load(well i dont know but i get no complaint but running load ... The 'loads' and 'dumps' methods use strings: ... If you want to store to / restore from file, ... I want to compare that cleanliness with other languages to compare ...
    (comp.lang.python)
  • Re: ufsdump/restore size limit?
    ... We're using ufs, and ufsdump and ufsrestore, dumping to an SDLT drive that's directly attached to the V240. ... We periodically restore the dumps to an old machine that we use for testing, just to make sure that they can be restored. ... Jeff Wieland ...
    (comp.sys.sun.admin)
  • Re: ufsdump/restore size limit?
    ... We're using ufs, and ufsdump and ufsrestore, dumping to an SDLT ... We are current with Solaris ... >> restore from that, ufsdump still asks for a second tape/file to restore ... It doesn't ask for a second tape for other dumps, ...
    (comp.sys.sun.admin)
  • Re: Looking for a Good FreeBSD and General Unix Backup System
    ... The interactive mode of restore, ... First, when you use interactive mode, you use shell-like commands ... has to do with the days when 1600 BPI 9-track tapes were backup media, ... and dumps often spanned multiple tapes, ...
    (freebsd-questions)
  • Re: Dump/Restore?
    ... Sorry, I was reading the restore man from my mac, and it was not as ... would be to move the massive dump file back over to the FreeBSD ... Let me know how you get on with it, I gzip my dumps and can pipe ...
    (freebsd-questions)