Re: save dictionary for later use?



On May 17, 3:52 am, castironpi <castiro...@xxxxxxxxx> wrote:
On May 16, 4:23 pm, Hans Nowak <zephyrfalcon!NO_SP...@xxxxxxxxx>
wrote:





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 seehttp://docs.python.org/lib/pickle-example.html.

Hope this helps!

--Hans

I want to compare that cleanliness with other languages to compare
formats.

Is pickle.load( open( 'out', 'rb' ) ) any better or worse than
pickle.load( 'out', 'rb' )?- Hide quoted text -

- Show quoted text -

This is a check-in on live-time writing. pickle.load didn't take two
parameters.
.



Relevant Pages

  • Re: How to test special relativity at home
    ... Harold Ensle: ... >> The second type of falsification concerns cases where travelers meet ... >> more than once and can compare the readings of their clocks. ... Which doesn't imply that any thinking went into the complaint. ...
    (sci.physics.relativity)
  • Re: New Protein Skimmer
    ... Prizm since I got my tank, not having anything to compare it to, I thought it worked just fine. ... The only complaint I had was it was noisy. ...
    (rec.aquaria.marine.reefs)
  • Re: Apologies (was Re: Itunes Comparison-*jpeg attached*
    ... >> For those that have not had the chance to compare! ... > I apologize, Wally. ... > I thought you had made the complaint before and now I realize it ...
    (comp.sys.mac.advocacy)
  • Re: Why cant we have/do that?
    ... disillusion us, can't it. ... Tokyo amazed me at it's cleanliness. ... compare that city to NY, ...
    (rec.travel.cruises)