471,050 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,050 software developers and data experts.

save dictionary for later use?

i extract info from one file and put it into a dictionary.
i want to save that dictionary for later use, how do i do that?
might save a list of dictionaries or a list of classobjects too if
there is any difference.
Jun 27 '08 #1
6 4491
On May 16, 2:17 pm, globalrev <skanem...@yahoo.sewrote:
i extract info from one file and put it into a dictionary.
i want to save that dictionary for later use, how do i do that?
might save a list of dictionaries or a list of classobjects too if
there is any difference.
use the 'pickle' module.
http://docs.python.org/lib/module-pickle.html

....
Jay Graves
Jun 27 '08 #2
On 16 Maj, 21:22, jay graves <jaywgra...@gmail.comwrote:
On May 16, 2:17 pm, globalrev <skanem...@yahoo.sewrote:
i extract info from one file and put it into a dictionary.
i want to save that dictionary for later use, how do i do that?
might save a list of dictionaries or a list of classobjects too if
there is any difference.

use the 'pickle' module.http://docs.python.org/lib/module-pickle.html

...
Jay Graves

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)
Jun 27 '08 #3
On May 16, 3:24 pm, globalrev <skanem...@yahoo.sewrote:
On 16 Maj, 21:22, jay graves <jaywgra...@gmail.comwrote:
On May 16, 2:17 pm, globalrev <skanem...@yahoo.sewrote:
i extract info from one file and put it into a dictionary.
i want to save that dictionary for later use, how do i do that?
might save a list of dictionaries or a list of classobjects too if
there is any difference.
use the 'pickle' module.http://docs.python.org/lib/module-pickle.html
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)
It's best to post a minimal set of code that exhibits your error.
You aren't saving the output of pickle.dumps and you are using
pickle.load instead of pickle.loads.

Sample loading to and from a string which you can tuck away in a file.
>>import pickle
test = {'a':1,'b':2}
picklestr = pickle.dumps(test)
test2 = pickle.loads(picklestr)
test == test2
True
>>>

Sample using an open file.
>>import pickle
test = {'a':1,'b':2}
pfile = open('pickletest','wb')
pickle.dump(test,pfile)
pfile.close()
pfile = open('pickletest','rb')
test2 = pickle.load(pfile)
pfile.close()
test == test2
True
>>>
....
Jay Graves
Jun 27 '08 #4
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'th at'\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
Jun 27 '08 #5
On May 16, 4:23*pm, Hans Nowak <zephyrfalcon!NO_SP...@gmail.com>
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'th at'\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' )?
Jun 27 '08 #6
On May 17, 3:52*am, castironpi <castiro...@gmail.comwrote:
On May 16, 4:23*pm, Hans Nowak <zephyrfalcon!NO_SP...@gmail.com>
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'th at'\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.
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by John Mudd | last post: by
10 posts views Thread by Luis P. Mendes | last post: by
4 posts views Thread by NullQwerty | last post: by
9 posts views Thread by nik | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.